Skip to content

Commit

Permalink
feat(langchain): add OpenRouterLanguageModel for API integration
Browse files Browse the repository at this point in the history
  • Loading branch information
mert-ergun committed Feb 12, 2025
1 parent 21bdb80 commit 56c7e8c
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions crossbar_llm/backend/tools/langchain_llm_qa_trial.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,23 @@ def __init__(
)


class OpenRouterLanguageModel:
"""
OpenRouterLanguageModel class for interacting with OpenRouter's language models.
It initializes the model with given API key and specified parameters.
"""

def __init__(
self, api_key: str, model_name: str = None, temperature: float | int = None,
base_url: str = "https://openrouter.ai/api/v1"
):
self.model_name = model_name or "deepseek/deepseek-r1"
self.temperature = temperature or 0
self.llm = ChatOpenAI(
api_key=api_key, model=self.model_name, temperature=self.temperature, request_timeout=600, base_url=base_url
)


class QueryChain:
"""
QueryChain class to handle the generation, correction, and parsing of Cypher queries using language models.
Expand Down Expand Up @@ -496,7 +513,7 @@ def define_llm(self, model_name):
model_name=model_name["cypher_llm_model"],
).llm
if model_name in openrouter_llm_models:
self.llm["cypher_llm"] = OpenAILanguageModel(
self.llm["cypher_llm"] = OpenRouterLanguageModel(
self.config.openrouter_api_key,
model_name=model_name["cypher_llm_model"],
).llm
Expand Down Expand Up @@ -531,7 +548,7 @@ def define_llm(self, model_name):
model_name=model_name["qa_llm_model"],
).llm
elif model_name in openrouter_llm_models:
self.llm["qa_llm"] = OpenAILanguageModel(
self.llm["qa_llm"] = OpenRouterLanguageModel(
self.config.openrouter_api_key,
model_name=model_name["qa_llm_model"],
).llm
Expand Down Expand Up @@ -561,7 +578,7 @@ def define_llm(self, model_name):
self.config.nvidia_api_key, model_name=model_name
).llm
elif model_name in openrouter_llm_models:
self.llm = OpenAILanguageModel(
self.llm = OpenRouterLanguageModel(
self.config.openrouter_api_key, model_name=model_name
).llm
else:
Expand Down

0 comments on commit 56c7e8c

Please sign in to comment.