Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added openrouter support #400

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/config.py
Expand Up @@ -45,7 +45,8 @@ def get_claude_api_key(self):

def get_openai_api_key(self):
return self.config["API_KEYS"]["OPENAI"]

def get_openrouter_api_key(self):
return self.config["API_KEYS"]["OPENROUTER"]
def get_gemini_api_key(self):
return self.config["API_KEYS"]["GEMINI"]

Expand Down
8 changes: 5 additions & 3 deletions src/llm/llm.py
Expand Up @@ -6,6 +6,7 @@
from .claude_client import Claude
from .openai_client import OpenAi
from .gemini_client import Gemini
from .openrouter_client import OpenRouter
from .mistral_client import MistralAi
from .groq_client import Groq

Expand Down Expand Up @@ -50,7 +51,8 @@ def __init__(self, model_id: str = None):
("GROQ LLAMA2 70B", "llama2-70b-4096"),
("GROQ GEMMA 7B IT", "gemma-7b-it"),
],
"OLLAMA": []
"OLLAMA": [],
"OPENROUTER":[("OpenRouter Default","Default")]
}
if ollama.client:
self.models["OLLAMA"] = [(model["name"].split(":")[0], model["name"]) for model in
Expand Down Expand Up @@ -88,7 +90,8 @@ def inference(self, prompt: str, project_name: str) -> str:
"OPENAI": OpenAi(),
"GOOGLE": Gemini(),
"MISTRAL": MistralAi(),
"GROQ": Groq()
"GROQ": Groq(),
"OPENROUTER" : OpenRouter()
}

try:
Expand All @@ -101,5 +104,4 @@ def inference(self, prompt: str, project_name: str) -> str:
logger.debug(f"Response ({model}): --> {response}")

self.update_global_token_usage(response, project_name)

return response
27 changes: 27 additions & 0 deletions src/llm/openrouter_client.py
@@ -0,0 +1,27 @@
from openai import OpenAI

from src.config import Config


class OpenRouter:
def __init__(self):
config = Config()
api_key = config.get_openrouter_api_key()
self.client = OpenAI(base_url="https://openrouter.ai/api/v1",api_key=api_key)

def inference(self, model_id: str, prompt: str) -> str:
chat_completion = self.client.chat.completions.create(
#should be uncommented if we want to show devika in openrouter
# extra_headers={
# "HTTP-Referer": "https://github.com/stitionai/devika", # Optional, for including your app on openrouter.ai rankings.
# "X-Title": "Devika", # Optional. Shows in rankings on openrouter.ai.
# },
messages=[
{
"role": "user",
"content": prompt.strip(),
}
],
model=model_id if model_id != "Default" else None,
)
return chat_completion.choices[0].message.content
3 changes: 2 additions & 1 deletion ui/src/routes/settings/+page.svelte
Expand Up @@ -18,7 +18,8 @@
"GEMINI": settings["API_KEYS"]["GEMINI"],
"MISTRAL": settings["API_KEYS"]["MISTRAL"],
"GROQ": settings["API_KEYS"]["GROQ"],
"NETLIFY": settings["API_KEYS"]["NETLIFY"]
"NETLIFY": settings["API_KEYS"]["NETLIFY"],
"OPENROUTER":settings["API_KEYS"]["OPENROUTER"]
};
// make a copy of the original settings
original = JSON.parse(JSON.stringify(settings));
Expand Down