forked from phoenix/litellm-mirror
Added openrouter.ai support
This commit is contained in:
parent
2463dc234b
commit
00c2384a2d
8 changed files with 118 additions and 21 deletions
5
.env.example
Normal file
5
.env.example
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
OPENAI_API_KEY = ""
|
||||||
|
COHERE_API_KEY = ""
|
||||||
|
OPENROUTER_API_KEY = ""
|
||||||
|
OR_SITE_URL = ""
|
||||||
|
OR_APP_NAME = "LiteLLM Example app"
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
.env
|
|
@ -1,4 +1,7 @@
|
||||||
import os, openai, cohere
|
import os, openai, cohere, dotenv
|
||||||
|
|
||||||
|
# Loading env variables using dotenv
|
||||||
|
dotenv.load_dotenv()
|
||||||
|
|
||||||
####### COMPLETION MODELS ###################
|
####### COMPLETION MODELS ###################
|
||||||
open_ai_chat_completion_models = [
|
open_ai_chat_completion_models = [
|
||||||
|
@ -13,6 +16,18 @@ cohere_models = [
|
||||||
'command-nightly',
|
'command-nightly',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
openrouter_models = [
|
||||||
|
'google/palm-2-codechat-bison',
|
||||||
|
'google/palm-2-chat-bison',
|
||||||
|
'openai/gpt-3.5-turbo',
|
||||||
|
'openai/gpt-3.5-turbo-16k',
|
||||||
|
'openai/gpt-4-32k',
|
||||||
|
'anthropic/claude-2',
|
||||||
|
'anthropic/claude-instant-v1',
|
||||||
|
'meta-llama/llama-2-13b-chat',
|
||||||
|
'meta-llama/llama-2-70b-chat'
|
||||||
|
]
|
||||||
|
|
||||||
####### EMBEDDING MODELS ###################
|
####### EMBEDDING MODELS ###################
|
||||||
open_ai_embedding_models = [
|
open_ai_embedding_models = [
|
||||||
'text-embedding-ada-002'
|
'text-embedding-ada-002'
|
||||||
|
@ -34,6 +49,32 @@ def completion(model, messages, azure=False):
|
||||||
engine=model,
|
engine=model,
|
||||||
messages = messages
|
messages = messages
|
||||||
)
|
)
|
||||||
|
elif "replicate" in model:
|
||||||
|
prompt = " ".join([message["content"] for message in messages])
|
||||||
|
output = replicate.run(
|
||||||
|
model,
|
||||||
|
input={
|
||||||
|
"prompt": prompt,
|
||||||
|
})
|
||||||
|
print(f"output: {output}")
|
||||||
|
response = ""
|
||||||
|
for item in output:
|
||||||
|
print(f"item: {item}")
|
||||||
|
response += item
|
||||||
|
new_response = {
|
||||||
|
"choices": [
|
||||||
|
{
|
||||||
|
"finish_reason": "stop",
|
||||||
|
"index": 0,
|
||||||
|
"message": {
|
||||||
|
"content": response,
|
||||||
|
"role": "assistant"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
print(f"new response: {new_response}")
|
||||||
|
response = new_response
|
||||||
elif model in cohere_models:
|
elif model in cohere_models:
|
||||||
cohere_key = os.environ.get("COHERE_API_KEY")
|
cohere_key = os.environ.get("COHERE_API_KEY")
|
||||||
co = cohere.Client(cohere_key)
|
co = cohere.Client(cohere_key)
|
||||||
|
@ -76,6 +117,22 @@ def completion(model, messages, azure=False):
|
||||||
model=model,
|
model=model,
|
||||||
prompt = prompt
|
prompt = prompt
|
||||||
)
|
)
|
||||||
|
|
||||||
|
elif model in openrouter_models:
|
||||||
|
openai.api_base = "https://openrouter.ai/api/v1"
|
||||||
|
openai.api_key = os.environ.get("OPENROUTER_API_KEY")
|
||||||
|
|
||||||
|
prompt = " ".join([message["content"] for message in messages])
|
||||||
|
|
||||||
|
response = openai.ChatCompletion.create(
|
||||||
|
model=model,
|
||||||
|
messages=messages,
|
||||||
|
headers={
|
||||||
|
"HTTP-Referer": os.environ.get("OR_SITE_URL"), # To identify your app
|
||||||
|
"X-Title": os.environ.get("OR_APP_NAME")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
reply = response.choices[0].message
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
@ -100,4 +157,3 @@ def embedding(model, input=[], azure=False):
|
||||||
|
|
||||||
#############################################
|
#############################################
|
||||||
#############################################
|
#############################################
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
from main import completion
|
from litellm.main import completion
|
||||||
import os
|
import os
|
||||||
|
|
||||||
## Configs for Models ##
|
## Configs for Models ##
|
||||||
|
@ -8,23 +8,27 @@ import os
|
||||||
|
|
||||||
|
|
||||||
messages = [{ "content": "Hello, how are you?","role": "user"}]
|
messages = [{ "content": "Hello, how are you?","role": "user"}]
|
||||||
# openai call
|
# # openai call
|
||||||
response = completion(model="gpt-3.5-turbo", messages=messages)
|
# response = completion(model="gpt-3.5-turbo", messages=messages)
|
||||||
print("\nOpenAI call")
|
# print("\nOpenAI call")
|
||||||
print(response)
|
# print(response)
|
||||||
|
|
||||||
# azure openai call
|
# # azure openai call
|
||||||
response = completion("chatgpt-test", messages, azure=True)
|
# response = completion("chatgpt-test", messages, azure=True)
|
||||||
print("\nAzure call")
|
# print("\nAzure call")
|
||||||
print(response)
|
# print(response)
|
||||||
|
|
||||||
# text davinci openai call
|
# # text davinci openai call
|
||||||
response = completion("text-davinci-003", messages)
|
# response = completion("text-davinci-003", messages)
|
||||||
print("\nDavinci call")
|
# print("\nDavinci call")
|
||||||
print(response)
|
# print(response)
|
||||||
|
|
||||||
# cohere call
|
# # cohere call
|
||||||
response = completion("command-nightly", messages)
|
# response = completion("command-nightly", messages)
|
||||||
print("\nCohere call")
|
# print("\nCohere call")
|
||||||
print(response)
|
# print(response)
|
||||||
|
|
||||||
|
# openrouter call
|
||||||
|
response = completion("google/palm-2-codechat-bison", messages)
|
||||||
|
print("\OpenRouter call")
|
||||||
|
print(response)
|
BIN
litellm/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
litellm/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
litellm/__pycache__/main.cpython-311.pyc
Normal file
BIN
litellm/__pycache__/main.cpython-311.pyc
Normal file
Binary file not shown.
|
@ -1,4 +1,7 @@
|
||||||
import os, openai, cohere
|
import os, openai, cohere, dotenv
|
||||||
|
|
||||||
|
# Loading env variables using dotenv
|
||||||
|
dotenv.load_dotenv()
|
||||||
|
|
||||||
####### COMPLETION MODELS ###################
|
####### COMPLETION MODELS ###################
|
||||||
open_ai_chat_completion_models = [
|
open_ai_chat_completion_models = [
|
||||||
|
@ -13,6 +16,18 @@ cohere_models = [
|
||||||
'command-nightly',
|
'command-nightly',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
openrouter_models = [
|
||||||
|
'google/palm-2-codechat-bison',
|
||||||
|
'google/palm-2-chat-bison',
|
||||||
|
'openai/gpt-3.5-turbo',
|
||||||
|
'openai/gpt-3.5-turbo-16k',
|
||||||
|
'openai/gpt-4-32k',
|
||||||
|
'anthropic/claude-2',
|
||||||
|
'anthropic/claude-instant-v1',
|
||||||
|
'meta-llama/llama-2-13b-chat',
|
||||||
|
'meta-llama/llama-2-70b-chat'
|
||||||
|
]
|
||||||
|
|
||||||
####### EMBEDDING MODELS ###################
|
####### EMBEDDING MODELS ###################
|
||||||
open_ai_embedding_models = [
|
open_ai_embedding_models = [
|
||||||
'text-embedding-ada-002'
|
'text-embedding-ada-002'
|
||||||
|
@ -102,6 +117,22 @@ def completion(model, messages, azure=False):
|
||||||
model=model,
|
model=model,
|
||||||
prompt = prompt
|
prompt = prompt
|
||||||
)
|
)
|
||||||
|
|
||||||
|
elif model in openrouter_models:
|
||||||
|
openai.api_base = "https://openrouter.ai/api/v1"
|
||||||
|
openai.api_key = os.environ.get("OPENROUTER_API_KEY")
|
||||||
|
|
||||||
|
prompt = " ".join([message["content"] for message in messages])
|
||||||
|
|
||||||
|
response = openai.ChatCompletion.create(
|
||||||
|
model=model,
|
||||||
|
messages=messages,
|
||||||
|
headers={
|
||||||
|
"HTTP-Referer": os.environ.get("OR_SITE_URL"), # To identify your app
|
||||||
|
"X-Title": os.environ.get("OR_APP_NAME")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
reply = response.choices[0].message
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
|
0
testos.py
Normal file
0
testos.py
Normal file
Loading…
Add table
Add a link
Reference in a new issue