นี่คือ wrapper API ที่ได้รับการออกแบบทางวิศวกรรมสำหรับสนามเด็กเล่น Vercel AI ซึ่งช่วยให้สามารถเข้าถึง LLMs ได้ฟรีรวมถึง OpenAI ของ CHATGPT ซึ่งเป็นคำสั่งของ Cohere 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
},
...
}
...
}
} โปรดทราบว่าเนื่องจากยังไม่มีการรับรองความถูกต้องหากโมเดลมีคุณสมบัติ "minBillingTier" ปัจจุบันจึงไม่สามารถใช้งานได้
รายการรหัสรุ่นยังมีอยู่ใน client.model_ids
>> > 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"
] dict ของพารามิเตอร์เริ่มต้นสำหรับแต่ละรุ่นสามารถพบได้ที่ 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 = {} - คำสั่งของพารามิเตอร์ตัวเลือก ดูส่วนก่อนหน้าสำหรับวิธีการค้นหาสิ่งเหล่านี้ฟังก์ชั่นเป็นเครื่องกำเนิดไฟฟ้าที่ส่งคืนข้อความที่สร้างขึ้นใหม่เป็นสตริง
ตัวอย่างสตรีม:
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 = {} - คำสั่งของพารามิเตอร์ตัวเลือก ดูส่วน "ดาวน์โหลดรุ่นที่มีอยู่" สำหรับวิธีการค้นหาสิ่งเหล่านี้ฟังก์ชั่นเป็นเครื่องกำเนิดไฟฟ้าที่ส่งคืนข้อความที่สร้างขึ้นใหม่เป็นสตริง
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/>.