Spaces:
Running
Running
File size: 9,635 Bytes
b917882 360f294 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
import json
from random import choices
import string
from langchain.tools import BaseTool
import requests
from dotenv import load_dotenv
from dataclasses import dataclass
from langchain_core.language_models.chat_models import BaseChatModel
from typing import (
Any,
Callable,
Dict,
List,
Literal,
Mapping,
Optional,
Sequence,
Type,
Union,
cast,
)
from langchain_core.callbacks import (
CallbackManagerForLLMRun,
)
from langchain_core.callbacks.manager import AsyncCallbackManagerForLLMRun
from langchain_core.exceptions import OutputParserException
from langchain_core.language_models import LanguageModelInput
from langchain_core.language_models.chat_models import BaseChatModel, LangSmithParams
from langchain_core.messages import (
AIMessage,
BaseMessage,
HumanMessage,
ToolMessage,
SystemMessage,
)
from langchain_core.outputs import ChatGeneration, ChatResult
from langchain_core.runnables import Runnable
from langchain_core.tools import BaseTool
class ChatGemini(BaseChatModel):
@property
def _llm_type(self) -> str:
"""Get the type of language model used by this chat model."""
return "gemini"
api_key :str
base_url:str = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent"
model_kwargs: Any = {}
def _generate(
self,
messages: list[BaseMessage],
stop: Optional[list[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> ChatResult:
"""Generate a chat response using the Gemini API.
This method handles both regular text responses and function calls.
For function calls, it returns a ToolMessage with structured function call data
that can be processed by Langchain's agent executor.
Function calls are returned with:
- tool_name: The name of the function to call
- tool_call_id: A unique identifier for the function call (name is used as Gemini doesn't provide one)
- content: The function arguments as a JSON string
- additional_kwargs: Contains the full function call details
Args:
messages: List of input messages
stop: Optional list of stop sequences
run_manager: Optional callback manager
**kwargs: Additional arguments passed to the Gemini API
Returns:
ChatResult containing either an AIMessage for text responses
or a ToolMessage for function calls
"""
# Convert messages to Gemini format
gemini_messages = []
system_message = None
for msg in messages:
# Handle both dict and LangChain message objects
if isinstance(msg, BaseMessage):
if isinstance(msg, SystemMessage):
system_message = msg.content
kwargs["system_instruction"]= {"parts":[{"text": system_message}]}
continue
if isinstance(msg, HumanMessage):
role = "user"
content = msg.content
elif isinstance(msg, AIMessage):
role = "model"
content = msg.content
elif isinstance(msg, ToolMessage):
# Handle tool messages by adding them as function outputs
gemini_messages.append(
{
"role": "model",
"parts": [{
"functionResponse": {
"name": msg.name,
"response": {"name": msg.name, "content": msg.content},
}}]}
)
continue
else:
role = "user" if msg["role"] == "human" else "model"
content = msg["content"]
message_part = {
"role": role,
"parts":[{"functionCall": { "name": msg.tool_calls[0]["name"], "args": msg.tool_calls[0]["args"]}}] if isinstance(msg, AIMessage) and msg.tool_calls else [{"text": content}]
}
gemini_messages.append(message_part)
# Prepare the request
headers = {
"Content-Type": "application/json"
}
params = {
"key": self.api_key
}
data = {
"contents": gemini_messages,
"generationConfig": {
"temperature": 0.7,
"topP": 0.8,
"topK": 40,
"maxOutputTokens": 2048,
},
**kwargs
}
try:
response = requests.post(
self.base_url,
headers=headers,
params=params,
json=data,
)
response.raise_for_status()
result = response.json()
if "candidates" in result and len(result["candidates"]) > 0 and "parts" in result["candidates"][0]["content"]:
parts = result["candidates"][0]["content"]["parts"]
tool_calls = []
content = ""
for part in parts:
if "text" in part:
content += part["text"]
if "functionCall" in part:
function_call = part["functionCall"]
tool_calls.append( {
"name": function_call["name"],
"id": function_call["name"]+random_string(5), # Gemini doesn't provide a unique id,}
"args": function_call["args"],
"type": "tool_call",})
# Create a proper ToolMessage with structured function call data
return ChatResult(generations=[
ChatGeneration(
message=AIMessage(
content=content,
tool_calls=tool_calls,
) if len(tool_calls) > 0 else AIMessage(content=content)
)
])
else:
raise Exception("No response generated")
except Exception as e:
raise Exception(f"Error calling Gemini API: {str(e)}")
def bind_tools(
self,
tools: Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]],
*,
tool_choice: Optional[Union[dict, str, Literal["auto", "any"], bool]] = None,
**kwargs: Any,
) -> Runnable[LanguageModelInput, BaseMessage]:
"""Bind tool-like objects to this chat model.
Args:
tools: A list of tool definitions to bind to this chat model.
Supports any tool definition handled by
:meth:`langchain_core.utils.function_calling.convert_to_openai_tool`.
tool_choice: If provided, which tool for model to call. **This parameter
is currently ignored as it is not supported by Ollama.**
kwargs: Any additional parameters are passed directly to
``self.bind(**kwargs)``.
"""
formatted_tools = {"function_declarations": [convert_to_gemini_tool(tool) for tool in tools]}
return super().bind(tools=formatted_tools, **kwargs)
def convert_to_gemini_tool(
tool: Union[BaseTool],
*,
strict: Optional[bool] = None,
) -> dict[str, Any]:
"""Convert a tool-like object to an Gemini tool schema.
Gemini tool schema reference:
https://ai.google.dev/gemini-api/docs/function-calling#function_calling_mode
Args:
tool:
BaseTool.
strict:
If True, model output is guaranteed to exactly match the JSON Schema
provided in the function definition. If None, ``strict`` argument will not
be included in tool definition.
Returns:
A dict version of the passed in tool which is compatible with the
Gemini tool-calling API.
"""
if isinstance(tool, BaseTool):
# Extract the tool's schema
schema = tool.args_schema.schema() if tool.args_schema else {"type": "object", "properties": {}}
#convert to gemini schema
raw_properties = schema.get("properties", {})
properties = {}
for key, value in raw_properties.items():
properties[key] = {
"type": value.get("type", "string"),
"description": value.get("title", ""),
}
# Build the function definition
function_def = {
"name": tool.name,
"description": tool.description,
"parameters": {
"type": "object",
"properties": properties,
"required": schema.get("required", [])
}
}
if strict is not None:
function_def["strict"] = strict
return function_def
else:
raise ValueError(f"Unsupported tool type: {type(tool)}")
def random_string(length: int) -> str:
return ''.join(choices(string.ascii_letters + string.digits, k=length))
|