vercel llm api
1.0.0
这是Vercel AI游乐场的逆向工程API包装器,它允许免费访问许多LLM,包括Openai的Chatgpt,Cohere's Commery Nightly以及一些开源型号。
用markdown-toc生成的目录。
您可以通过运行以下命令来安装此库:
pip3 install vercel-llm-api
示例可以在/examples目录中找到。要运行这些示例,只需从终端执行随附的Python文件即可。
python3 examples/generate.py
要使用此库,只需导入vercel_ai并创建一个vercel_ai.Client实例。您可以使用proxy关键字参数指定代理。
正常示例:
import vercel_ai
client = vercel_ai . Client ()代理示例:
import vercel_ai
client = vercel_ai . Client ( proxy = "socks5h://193.29.62.48:11003" )请注意,以下示例假设client是您的vercel_ai.Client实例的名称。
客户在初始化时下载可用模型,并将其存储在client.models中。
>> > print ( json . dumps ( client . models , indent = 2 ))
{
"anthropic:claude-instant-v1" : {
"id" : "anthropic:claude-instant-v1" , #the model's id
"provider" : "anthropic" , #the model's provider
"providerHumanName" : "Anthropic" , #the provider's display name
"makerHumanName" : "Anthropic" , #the maker of the model
"minBillingTier" : "hobby" , #the minimum billing tier needed to use the model
"parameters" : { #a dict of optional parameters that can be passed to the generate function
"temperature" : { #the name of the parameter
"value" : 1 , #the default value for the parameter
"range" : [ 0 , 1 ] #a range of possible values for the parameter
},
...
}
...
}
}请注意,由于尚无AUTH,因此如果模型具有"minBillingTier"属性,则无法使用它。
client.model_ids中也可用模型ID列表。
>> > print ( json . dumps ( client . model_ids , indent = 2 ))
[
"anthropic:claude-instant-v1" , #locked to hobby tier; unusable
"anthropic:claude-v1" , #locked to hobby tier; unusable
"replicate:replicate/alpaca-7b" ,
"replicate:stability-ai/stablelm-tuned-alpha-7b" ,
"huggingface:bigscience/bloom" ,
"huggingface:bigscience/bloomz" ,
"huggingface:google/flan-t5-xxl" ,
"huggingface:google/flan-ul2" ,
"huggingface:EleutherAI/gpt-neox-20b" ,
"huggingface:OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5" ,
"huggingface:bigcode/santacoder" ,
"cohere:command-medium-nightly" ,
"cohere:command-xlarge-nightly" ,
"openai:gpt-4" , #locked to pro tier; unusable
"openai:code-cushman-001" ,
"openai:code-davinci-002" ,
"openai:gpt-3.5-turbo" ,
"openai:text-ada-001" ,
"openai:text-babbage-001" ,
"openai:text-curie-001" ,
"openai:text-davinci-002" ,
"openai:text-davinci-003"
]可以在client.model_params上找到每个模型的默认参数的命令。
>> > print ( json . dumps ( client . model_defaults , indent = 2 ))
{
"anthropic:claude-instant-v1" : {
"temperature" : 1 ,
"maximumLength" : 200 ,
"topP" : 1 ,
"topK" : 1 ,
"presencePenalty" : 1 ,
"frequencyPenalty" : 1 ,
"stopSequences" : [
" n n Human:"
]
},
...
}要生成一些文本,请使用client.generate函数,该函数接受以下参数:
model - 您要使用的模型的ID。prompt - 您的提示。params = {} - 可选参数的dist。有关如何找到这些信息,请参见上一节。该函数是一个发电机,将新生成的文本返回为字符串。
流示例:
for chunk in client . generate ( "openai:gpt-3.5-turbo" , "Summarize the GNU GPL v3" ):
print ( chunk , end = "" , flush = True )非流程示例:
result = ""
for chunk in client . generate ( "openai:gpt-3.5-turbo" , "Summarize the GNU GPL v3" ):
result += chunk
print ( result )要生成聊天消息,请使用client.chat函数,该函数接受以下参数:
model - 您要使用的模型的ID。messages - 消息列表。这种格式与您如何使用官方OpenAI API相同。params = {} - 可选参数的dist。有关如何找到这些内容,请参见“下载可用模型”部分。该函数是一个发电机,将新生成的文本返回为字符串。
messages = [
{ "role" : "system" , "content" : "You are a helpful assistant." },
{ "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?" }
]
for chunk in client . chat ( "openai:gpt-3.5-turbo" , messages ):
print ( chunk , end = "" , flush = True )
print ()如果要显示调试消息,只需致电vercel_ai.logger.setLevel 。
import vercel_ai
import logging
vercel_ai . logger . setLevel ( logging . INFO )该程序在GNU GPL V3下获得许可。所有代码都是由我撰写的,Ading2210。
ading2210/vercel-llm-api: a reverse engineered API wrapper for the Vercel AI Playground
Copyright (C) 2023 ading2210
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.