File size: 15,109 Bytes
3319147 | 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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | import openai
import json
from typing import Dict, List, Optional, Any, Union
import asyncio
from langchain_core.language_models.llms import LLM
from langchain_core.language_models.chat_models import BaseChatModel
from langchain_core.messages import BaseMessage, HumanMessage, AIMessage, SystemMessage
from langchain_core.outputs import ChatGeneration, ChatResult
from langchain_core.callbacks.manager import CallbackManagerForLLMRun
from pydantic import Field
class OpenRouterLLMClient:
def __init__(self, api_key: str, base_url: str = "https://openrouter.ai/api/v1"):
self.api_key = api_key
self.base_url = base_url
self.client = openai.OpenAI(
api_key=api_key,
base_url=base_url
)
self.async_client = openai.AsyncOpenAI(
api_key=api_key,
base_url=base_url
)
def generate_response(
self,
messages: List[Dict[str, str]],
provider: str,
model: str,
max_tokens: int = 4000,
temperature: float = 0.7,
top_p: float = 1.0,
**kwargs
) -> str:
"""Generate a response using the specified provider and model."""
# Format model name for OpenRouter (provider/model)
model_name = f"{provider}/{model}"
params = {
"model": model_name,
"messages": messages,
"max_tokens": max_tokens,
"temperature": temperature,
"top_p": top_p,
**kwargs
}
try:
response = self.client.chat.completions.create(**params)
return response.choices[0].message.content
except Exception as e:
raise Exception(f"Error generating response with {provider}/{model}: {str(e)}")
async def generate_response_async(
self,
messages: List[Dict[str, str]],
provider: str,
model: str,
max_tokens: int = 4000,
temperature: float = 0.7,
top_p: float = 1.0,
**kwargs
) -> str:
"""Async version of generate_response."""
model_name = f"{provider}/{model}"
params = {
"model": model_name,
"messages": messages,
"max_tokens": max_tokens,
"temperature": temperature,
"top_p": top_p,
**kwargs
}
try:
response = await self.async_client.chat.completions.create(**params)
return response.choices[0].message.content
except Exception as e:
raise Exception(f"Error generating response with {provider}/{model}: {str(e)}")
def stream_response(
self,
messages: List[Dict[str, str]],
provider: str,
model: str,
max_tokens: int = 4000,
temperature: float = 0.7,
top_p: float = 1.0,
**kwargs
):
"""Stream response from the specified provider and model."""
model_name = f"{provider}/{model}"
params = {
"model": model_name,
"messages": messages,
"max_tokens": max_tokens,
"temperature": temperature,
"top_p": top_p,
"stream": True,
**kwargs
}
try:
stream = self.client.chat.completions.create(**params)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
yield chunk.choices[0].delta.content
except Exception as e:
raise Exception(f"Error streaming response with {provider}/{model}: {str(e)}")
# Agent-compatible wrapper class
class LLMAgent:
def __init__(self, llm_client: OpenRouterLLMClient, default_provider: str, default_model: str):
self.llm_client = llm_client
self.default_provider = default_provider
self.default_model = default_model
self.conversation_history = []
def __call__(self, message: str, provider: Optional[str] = None, model: Optional[str] = None, **kwargs) -> str:
"""Make the agent callable - solves binding issues for agent frameworks."""
return self.chat(message, provider, model, **kwargs)
def chat(self, message: str, provider: Optional[str] = None, model: Optional[str] = None, **kwargs) -> str:
"""Chat with the LLM using the specified or default provider/model."""
provider = provider or self.default_provider
model = model or self.default_model
# Add user message to history
self.conversation_history.append({"role": "user", "content": message})
try:
response = self.llm_client.generate_response(
messages=self.conversation_history,
provider=provider,
model=model,
**kwargs
)
# Add assistant response to history
self.conversation_history.append({"role": "assistant", "content": response})
return response
except Exception as e:
return f"Error: {str(e)}"
async def chat_async(self, message: str, provider: Optional[str] = None, model: Optional[str] = None, **kwargs) -> str:
"""Async version of chat."""
provider = provider or self.default_provider
model = model or self.default_model
self.conversation_history.append({"role": "user", "content": message})
try:
response = await self.llm_client.generate_response_async(
messages=self.conversation_history,
provider=provider,
model=model,
**kwargs
)
self.conversation_history.append({"role": "assistant", "content": response})
return response
except Exception as e:
return f"Error: {str(e)}"
def stream_chat(self, message: str, provider: Optional[str] = None, model: Optional[str] = None, **kwargs):
"""Stream chat response."""
provider = provider or self.default_provider
model = model or self.default_model
self.conversation_history.append({"role": "user", "content": message})
try:
full_response = ""
for chunk in self.llm_client.stream_response(
messages=self.conversation_history,
provider=provider,
model=model,
**kwargs
):
full_response += chunk
yield chunk
# Add complete response to history
self.conversation_history.append({"role": "assistant", "content": full_response})
except Exception as e:
yield f"Error: {str(e)}"
def clear_history(self):
"""Clear conversation history."""
self.conversation_history = []
def get_history(self) -> List[Dict[str, str]]:
"""Get conversation history."""
return self.conversation_history.copy()
def set_system_message(self, system_message: str):
"""Set or update system message."""
# Remove existing system message if present
if self.conversation_history and self.conversation_history[0]["role"] == "system":
self.conversation_history.pop(0)
# Add new system message at the beginning
self.conversation_history.insert(0, {"role": "system", "content": system_message})
# --- SUBSET OF CODE TO REPLACE ---
# LangChain-compatible wrapper
class OpenRouterChatModel(BaseChatModel):
"""LangChain-compatible chat model wrapper for OpenRouter."""
llm_client: OpenRouterLLMClient = Field(...)
provider: str = Field(...)
model: str = Field(...)
max_tokens: int = Field(default=4000)
temperature: float = Field(default=0.7)
top_p: float = Field(default=1.0)
class Config:
arbitrary_types_allowed = True
@property
def _llm_type(self) -> str:
return "openrouter_chat"
def _convert_messages_to_openai_format(self, messages: List[BaseMessage]) -> List[Dict[str, str]]:
"""Convert LangChain messages to the OpenAI API format, correctly handling tool calls."""
openai_messages = []
for message in messages:
if isinstance(message, HumanMessage):
openai_messages.append({"role": "user", "content": message.content})
elif isinstance(message, AIMessage):
msg_dict = {"role": "assistant", "content": message.content or ""}
if message.tool_calls:
# This part now correctly receives a list of dicts and converts them
# to the format the API client expects for a new request.
msg_dict["tool_calls"] = [
{
"id": tc["id"],
"type": "function",
"function": {
"name": tc["name"],
"arguments": json.dumps(tc["args"]),
},
}
for tc in message.tool_calls
]
openai_messages.append(msg_dict)
elif isinstance(message, SystemMessage):
openai_messages.append({"role": "system", "content": message.content})
elif hasattr(message, "tool_call_id"): # Catches ToolMessage robustly
openai_messages.append({
"role": "tool",
"tool_call_id": message.tool_call_id,
"content": message.content,
})
else:
openai_messages.append({"role": "user", "content": str(message.content)})
return openai_messages
# This is a helper to centralize the logic for both sync and async calls
def _prepare_and_run_api(self, messages: List[BaseMessage], **kwargs: Any) -> openai.types.chat.ChatCompletion:
openai_messages = self._convert_messages_to_openai_format(messages)
# Merge instance parameters with kwargs
params = {
"max_tokens": kwargs.get("max_tokens", self.max_tokens),
"temperature": kwargs.get("temperature", self.temperature),
"top_p": kwargs.get("top_p", self.top_p),
}
# --- NEW: THIS IS THE CRITICAL CHANGE ---
# The agent passes tools via kwargs. We must grab them and add to the API params.
if "tools" in kwargs:
params["tools"] = kwargs["tools"]
# The client needs the raw response to check for tool calls, so we don't just return content
model_name = f"{self.provider}/{self.model}"
return self.llm_client.client.chat.completions.create(
model=model_name,
messages=openai_messages,
**params
)
async def _aprepare_and_run_api(self, messages: List[BaseMessage], **kwargs: Any) -> openai.types.chat.ChatCompletion:
openai_messages = self._convert_messages_to_openai_format(messages)
params = {
"max_tokens": kwargs.get("max_tokens", self.max_tokens),
"temperature": kwargs.get("temperature", self.temperature),
"top_p": kwargs.get("top_p", self.top_p),
}
if "tools" in kwargs:
params["tools"] = kwargs["tools"]
model_name = f"{self.provider}/{self.model}"
return await self.llm_client.async_client.chat.completions.create(
model=model_name,
messages=openai_messages,
**params
)
def _generate(
self,
messages: List[BaseMessage],
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> ChatResult:
"""Generate chat response."""
try:
response = self._prepare_and_run_api(messages, **kwargs)
response_message = response.choices[0].message
# THIS IS THE NEW, CRITICAL PARSING STEP
parsed_tool_calls = []
if response_message.tool_calls:
for tc in response_message.tool_calls:
parsed_tool_calls.append({
"name": tc.function.name,
"args": json.loads(tc.function.arguments),
"id": tc.id,
})
message = AIMessage(
content=response_message.content or "",
tool_calls=parsed_tool_calls # Pass the parsed list of dicts
)
generation = ChatGeneration(message=message)
return ChatResult(generations=[generation])
except Exception as e:
raise Exception(f"Error in OpenRouter chat generation: {str(e)}")
async def _agenerate(
self,
messages: List[BaseMessage],
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> ChatResult:
"""Async generate chat response."""
try:
response = await self._aprepare_and_run_api(messages, **kwargs)
response_message = response.choices[0].message
# THIS IS THE NEW, CRITICAL PARSING STEP
parsed_tool_calls = []
if response_message.tool_calls:
for tc in response_message.tool_calls:
parsed_tool_calls.append({
"name": tc.function.name,
"args": json.loads(tc.function.arguments),
"id": tc.id,
})
message = AIMessage(
content=response_message.content or "",
tool_calls=parsed_tool_calls # Pass the parsed list of dicts
)
generation = ChatGeneration(message=message)
return ChatResult(generations=[generation])
except Exception as e:
raise Exception(f"Error in OpenRouter async chat generation: {str(e)}")
# Convenience function to create LangChain-compatible LLM
def create_openrouter_llm(api_key: str, provider: str, model: str, **kwargs) -> OpenRouterChatModel:
"""Create a LangChain-compatible OpenRouter LLM."""
llm_client = OpenRouterLLMClient(api_key)
return OpenRouterChatModel(
llm_client=llm_client,
provider=provider,
model=model,
**kwargs
)
|