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 OpenAI Compatible Support #661

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sample.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ GEMINI = "<YOUR_GEMINI_API_KEY>"
MISTRAL = "<YOUR_MISTRAL_API_KEY>"
GROQ = "<YOUR_GROQ_API_KEY>"
NETLIFY = "<YOUR_NETLIFY_API_KEY>"
NEWAI = "<YOUR_NEWA_API_KEY>"

[API_ENDPOINTS]
BING = "https://api.bing.microsoft.com/v7.0/search"
GOOGLE = "https://www.googleapis.com/customsearch/v1"
OLLAMA = "http://127.0.0.1:11434"
NEWAI = "https://api.newai.com/v1"

LM_STUDIO = "http://localhost:1234/v1"
OPENAI = "https://api.openai.com/v1"
Expand Down
14 changes: 14 additions & 0 deletions src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,20 @@ def set_timeout_inference(self, value):
def save_config(self):
with open("config.toml", "w") as f:
toml.dump(self.config, f)

def get_newai_api_key(self):
return self.config["API_KEYS"]["NEWAI"]

def get_newai_api_endpoint(self):
return self.config["API_ENDPOINTS"]["NEWAI"]

def set_newai_api_key(self, key):
self.config["API_KEYS"]["NEWAI"] = key
self.save_config()

def set_newai_api_endpoint(self, endpoint):
self.config["API_ENDPOINTS"]["NEWAI"] = endpoint
self.save_config()

def update_config(self, data):
for key, value in data.items():
Expand Down
23 changes: 23 additions & 0 deletions src/llm/newai_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from openai import OpenAI

from src.config import Config

class NewAi:
def __init__(self):
config = Config()
api_key = config.get_newai_api_key()
base_url = config.get_newai_api_endpoint()
self.client = OpenAI(api_key=api_key, base_url=base_url)

def inference(self, model_id: str, prompt: str) -> str:
chat_completion = self.client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt.strip(),
}
],
model=model_id,
temperature=0
)
return chat_completion.choices[0].message.content