我們的使命是簡化LLM景觀,統一讓您:
?使用來自任何提供商的任何LLM :使用單個接口,您可以通過簡單地更改一個字符串來使用所有提供商的所有LLM。無需管理多個API鍵或處理不同的輸入輸出格式。統一為您處理所有這些!
提高LLM性能:添加您自己的自定義測試和EVALS,並在所有型號和提供商上基準提示自己的提示。比較質量,成本和速度,並在系統提示上進行迭代,直到所有測試用例通過為止,您可以部署應用程序!
?通往最佳LLM的途徑:通過將每個提示的理想模型和提供商路由來提高質量,成本和速度。
只需安裝軟件包:
pip install unifyai
然後註冊以獲取您的API鍵,然後就可以了!
import unify
client = unify . Unify ( "gpt-4o@openai" , api_key = < your_key > )
client . generate ( "hello world!" )
筆記
我們建議使用python-dotenv將UNIFY_KEY="My API Key"
添加到.env
文件中,以避免使用上述使用api_key
參數。對於其餘的讀數,我們將假設您將密鑰設置為環境變量。
您可以列出所有模型,提供商和端點( <model>@<provider>
Pair),如下:
models = unify . list_models ()
providers = unify . list_providers ()
endpoints = unify . list_endpoints ()
您也可以在這些功能中過濾如下:
import random
anthropic_models = unify . list_models ( "anthropic" )
client . set_endpoint ( random . choice ( anthropic_models ) + "@anthropic" )
latest_llama3p1_providers = unify . list_providers ( "llama-3.1-405b-chat" )
client . set_endpoint ( "llama-3.1-405b-chat@" + random . choice ( latest_llama3p1_providers ))
openai_endpoints = unify . list_endpoints ( "openai" )
client . set_endpoint ( random . choice ( openai_endpoints ))
mixtral8x7b_endpoints = unify . list_endpoints ( "mixtral-8x7b-instruct-v0.1" )
client . set_endpoint ( random . choice ( mixtral8x7b_endpoints ))
如果要更改endpoint
, model
或provider
,則可以分別使用.set_endpoint
, .set_model
, .set_provider
方法來完成。
client . set_endpoint ( "mistral-7b-instruct-v0.3@deepinfra" )
client . set_model ( "mistral-7b-instruct-v0.3" )
client . set_provider ( "deepinfra" )
您可以使用system_message
參數在.generate
函數中影響模型的角色:
response = client . generate (
user_message = "Hello Llama! Who was Isaac Newton?" , system_message = "You should always talk in rhymes"
)
如果您想使用.generate
函數發送多個消息,則應使用以下messages
參數:
messages = [
{ "role" : "user" , "content" : "Who won the world series in 2020?" },
{ "role" : "assistant" , "content" : "The Los Angeles Dodgers won the World Series in 2020." },
{ "role" : "user" , "content" : "Where was it played?" }
]
res = client . generate ( messages = messages )
查詢LLMS時,您通常需要保留提示的許多方面,並且僅在每個後續呼叫上更改提示的一小部分。
例如,您可能需要修復溫帶,系統提示和可用工具,同時傳遞來自下游應用程序的不同用戶消息。統一中的所有客戶端都可以通過默認參數非常簡單,該參數可以在構造函數中指定,也可以使用setters方法在任何時間設置。
例如,以下代碼將將temperature=0.5
傳遞給所有後續請求,而無需重複傳遞到.generate()
方法中。
client = unify . Unify ( "claude-3-haiku@anthropic" , temperature = 0.5 )
client . generate ( "Hello world!" )
client . generate ( "What a nice day." )
所有參數也可以通過getters檢索,並通過setters設置:
client = unify . Unify ( "claude-3-haiku@anthropic" , temperature = 0.5 )
print ( client . temperature ) # 0.5
client . set_temperature ( 1.0 )
print ( client . temperature ) # 1.0
將值傳遞給.generate()
方法將覆蓋為客戶端指定的默認值。
client = unify . Unify ( "claude-3-haiku@anthropic" , temperature = 0.5 )
client . generate ( "Hello world!" ) # temperature of 0.5
client . generate ( "What a nice day." , temperature = 1.0 ) # temperature of 1.0
為了同時處理多個用戶請求,例如在聊天機器人應用程序中,建議對它們進行異步處理。下面給出了使用AsyncUnify
的最小示例:
import unify
import asyncio
async_client = unify . AsyncUnify ( "llama-3-8b-chat@fireworks-ai" )
asyncio . run ( async_client . generate ( "Hello Llama! Who was Isaac Newton?" ))
更多一個更應用的示例,可以並行處理多個請求如下:
import unify
import asyncio
clients = dict ()
clients [ "gpt-4o@openai" ] = unify . AsyncUnify ( "gpt-4o@openai" )
clients [ "claude-3-opus@anthropic" ] = unify . AsyncUnify ( "claude-3-opus@anthropic" )
clients [ "llama-3-8b-chat@fireworks-ai" ] = unify . AsyncUnify ( "llama-3-8b-chat@fireworks-ai" )
async def generate_responses ( user_message : str ):
responses_ = dict ()
for endpoint_ , client in clients . items ():
responses_ [ endpoint_ ] = await client . generate ( user_message )
return responses_
responses = asyncio . run ( generate_responses ( "Hello, how's it going?" ))
for endpoint , response in responses . items ():
print ( "endpoint: {}" . format ( endpoint ))
print ( "response: {} n " . format ( response ))
明智的功能,異步和同步客戶端是相同的。
您可以通過在.generate
函數中設置stream=True
來啟用流響應。
import unify
client = unify . Unify ( "llama-3-8b-chat@fireworks-ai" )
stream = client . generate ( "Hello Llama! Who was Isaac Newton?" , stream = True )
for chunk in stream :
print ( chunk , end = "" )
它與異步客戶端完全相同。
import unify
import asyncio
async_client = unify . AsyncUnify ( "llama-3-8b-chat@fireworks-ai" )
async def stream ():
async_stream = await async_client . generate ( "Hello Llama! Who was Isaac Newton?" , stream = True )
async for chunk in async_stream :
print ( chunk , end = "" )
asyncio . run ( stream ())
要了解有關我們更高級的API功能,基準測試和LLM路由的更多信息,請查看我們的全面文檔!