หมายเหตุ: ไลบรารีนี้เลิกใช้แล้วตอนนี้แบ่งออกเป็น ZOD-GPT และ LLM-API

ชุดเครื่องมือวิศวกรรมพรอมต์ TypeScript-First สำหรับการทำงานกับโมเดลภาษาขนาดใหญ่ที่ใช้แชท (LLMS)
Llamaflow เป็นเลเยอร์มิดเดิลแวร์ที่อยู่ระหว่างซอฟต์แวร์ของคุณและโมเดล AI มันเพิ่มความสามารถต่อไปนี้ที่ด้านบนของ API การแชทมาตรฐานเสร็จสิ้น:
ด้วย llamaflow คุณสามารถสอบถามโมเดล chatgpt ของ Openai ได้เช่น So:
import { OpenAI } from 'llama-flow' ;
const model = new OpenAI ( { apiKey : 'YOUR_OPENAI_KEY' } ) ;
const chat = model . chat ( {
systemMessage :
"You are a smart and honest AI assistant. Follow the user's requirements carefully & to the letter, minimize any other prose." ,
} ) ;
const response = await chat . request (
prompt . json ( {
message :
'What are some good names for childrens book about the renaissance? Respond as a JSON array' ,
schema : z . array ( z . string ( ) . max ( 200 ) ) ,
} ) ,
) ;
console . log ( response . content ) ; // content will be typed as string[]; แพ็คเกจนี้โฮสต์บน NPM:
npm i llama-flow
yarn add llama-flow
ในการตั้งค่าใน codebase ของคุณให้เริ่มต้นอิน OpenAI แตนซ์ใหม่ด้วยโมเดลที่คุณต้องการ โปรดทราบว่าคุณสามารถเพิ่มโมเดลเริ่มต้นและการตั้งค่าการแชท (เช่นอุณหภูมิการหมดเวลาการลองใหม่) เมื่อเริ่มต้น สิ่งเหล่านี้เป็นเพียงค่าเริ่มต้นและสามารถเขียนทับได้ในภายหลังตามแบบต่อการแชทหรือตามคำตอบ
import { OpenAI } from 'llama-flow' ;
const model = new OpenAI (
{ apiKey : 'YOUR_OPENAI_KEY' } ,
{ model : 'gpt-3.5-turbo' } ,
) ;การแชทคือการสนทนาระหว่าง "ผู้ใช้" (ซอฟต์แวร์ของคุณ) และ AI Agent Llamaflow จะดูแลการจัดการหน่วยความจำแชทดังนั้นคุณสามารถสนทนาต่อไปได้โดยส่งคำขออื่น โปรดทราบว่าจะมีการเพิ่มกลยุทธ์การจัดการหน่วยความจำที่แตกต่างกันในอนาคตเช่นการตัดแต่งหน่วยความจำตามต้องการเพื่อให้พอดีกับหน้าต่างบริบท
const chat = model . chat ( {
systemMessage : 'You are an AI writer.' ,
retainMemory : true ,
} ) ;
// You can ask the AI model with a simple string, or a dedicated `Prompt` object.
const response = await chat . request (
prompt . text (
'Write a script for a tiktok video that talks about the artistic contribution of the renaissance.' ,
) ,
) ;
// The results, as well as any usage stats, will be returned.
console . log (
`The AI writer's response is: ${ response . content } . Token used: ${ response . usage . totalTokens } .` ,
) ;
// You can follow up on this chat by prompting further, using the `bulletPrompt` object that was created earlier.
const bulletPoints = await chat . request ( bulletPrompt ) ;
// `bulletPoints.content` will be automatically casted in the correct type as defined in the schema field of `bulletPrompt`
console . log (
`The structured version of this response is: ${ JSON . stringify (
bulletPoints . content ,
) } ` ,
) ;พรอมต์เป็นข้อความถึงการแชท AI โดยมีความคาดหวังของรูปแบบการตอบกลับที่เฉพาะเจาะจง ข้อความประเภทพรอมต์ได้รับการตรวจสอบความถูกต้องเพื่อให้แน่ใจว่าการจัดรูปแบบที่กำหนดจะถูกส่งคืนอย่างแน่นอนหรือจะผิดพลาด มีการแจ้งเตือนประเภทต่าง ๆ สำหรับรูปแบบที่แตกต่างกัน นี่คือตัวอย่างของพรอมต์ JSON
import { prompt } from 'llama-flow' ;
import { z } from 'zod' ; // JSON prompt uses Zod for schema validation.
const bulletPrompt = prompt . json ( {
message :
'Please rewrite this in a list of bullet points. Respond as a JSON array, where each element in the array is one bullet point. Keep each bullet point to be 200 characters max. For example: ["bullet point 1", "bullet point 2"]' ,
schema : z . array ( z . string ( ) . max ( 200 ) ) ,
} ) ; โปรดทราบว่าวัตถุ Prompt แยกออกจาก message หลักและ formatMessage สิ่งนี้ใช้สำหรับการลองใหม่ เมื่อ Llamaflow ใช้พรอมต์นี้มันจะถามโมเดลทั้งข้อความหลักและรูปแบบ หากโมเดลกลับมาพร้อมกับการตอบกลับที่จัดรูปแบบไม่ถูกต้องจะขอให้โมเดลแก้ไขเอาต์พุตก่อนหน้าโดยใช้ formatMessage เท่านั้น
คุณสามารถสร้างวัตถุพรอมต์ของคุณเองด้วยตัวตรวจสอบที่กำหนดเองได้เช่นกัน Llamaflow เป็นวิธีที่ง่ายและยืดหยุ่นในการสร้างผู้ตรวจสอบทุกประเภท นี่คือตัวอย่างบางส่วนของตัวตรวจสอบที่กำหนดเอง:
รับตัวอย่างที่รวดเร็วด้านบน แต่คราวนี้มันจะขอให้แบบจำลองเพียงแค่ตอบกลับในกระสุนจริงแทนที่จะเป็นอาร์เรย์ JSON สิ่งนี้มีประโยชน์เพราะบางครั้งโมเดล (ESP <GPT-4) ไม่ได้ดีที่สุดในการทำตามคำแนะนำการจัดรูปแบบเฉพาะต่อไปนี้โดยเฉพาะอย่างยิ่งเมื่อมันมาถึงโครงสร้างข้อมูลที่ซับซ้อน
import { prompt } from 'llama-flow' ;
const bulletPrompt = prompt . json ( {
message :
'Please rewrite this in a list of bullet points. Respond as a list of bullet points, where each bullet point begins with the "-" character. Each bullet point should be less than 200 characters. Put each bullet point on a new line.' ,
// parse the response from the model so it can be fed into the schema validator
parseResponse : ( res ) => res . split ( 'n' ) . map ( ( s ) => s . replace ( '-' , '' ) . trim ( ) ) ,
// it's useful to define custom error messages, any schema parse errors will be automatically fed back into the model on retry, so the model knows exactly what to correct.
schema : z . array (
z . string ( ) . max ( 200 , {
message : 'This bullet point should be less than 200 characters.' ,
} ) ,
) ,
} ) ; ตอนนี้เรามาทำสิ่งนี้ต่อไป คุณสามารถสร้างพรอมต์ที่ใช้โมเดล (หรือแหล่งข้อมูลภายนอกอื่น ๆ ) เพื่อตรวจสอบความถูกต้องของเอาต์พุตของตัวเอง คุณสามารถทำได้โดยผ่านวิธี validate ของ Async ที่กำหนดเอง โปรดทราบว่าวิธีนี้จะแทนที่คุณสมบัติที่เกี่ยวข้องกับการตรวจสอบความถูกต้องอื่น ๆ เช่น formatMessage , parseResponse , schema .. ฯลฯ
import { prompt , Chat } from 'llama-flow' ;
const factCheckerChat = model . chat ( {
systemMessage :
'You are a fact checker that responds to if the user's messages are true or not, with just the word "true" or "false". Do not add punctuations or any other text. If the user asks a question, request, or anything that cannot be fact checked, ignore the user's request and just say "null".' ,
// The fact checker is designed to fulfill each request independently (e.g. the current request does not depend on the content of the previous request). So no need to keep message memory to save on tokens.
retainMemory : false ,
} ) ;
const buildFactCheckedPrompt = ( article : string ) =>
prompt . text ( {
message : `Please write a summary about the following article: ${ article } ` ,
// Because LLM driven validation can get expensive, set a lower retry count.
promptRetries : 2 ,
parse : async ( response ) => {
// Check if this summary is true or not
const { response } = await factCheckerChat . request (
prompt . boolean ( {
message : response . content ,
} ) ,
) ;
if ( response . content === true ) {
return { success : true , data : response . content } ;
} else {
// if `retryPrompt` is set, LLamaFlow will automatically retry with the text in this property.
return {
success : false ,
retryPrompt :
'This summary is not true, please rewrite with only true facts.' ,
} ;
}
} ,
} ) ;
// now, every content generated by this chat will be fact checked by the LLM itself, and this request will throw an error if the content can't be fixed (once the maximum number of retries has been reached).
const factCheckedContent = await chat . request (
buildFactCheckedPrompt (
'Write a script for a tiktok video that talks about the artistic contribution of the renaissance.' ,
) ,
) ;เนื่องจากนี่คือ API จึงมักจะมีประโยชน์ในการร้องขอจากการแชทเดียวกัน บ่อยครั้งที่ประวัติข้อความจะทำหน้าที่เป็นบริบทสำหรับคำขอต่อไป ตัวอย่างการใช้งานที่ดีคือพร้อมที่จะเขียนเนื้อหาก่อนจากนั้นแยกเอนทิตีและสุดท้ายให้ตัวเลือกบางอย่างสำหรับชื่อเรื่อง
// You can reset chat history anytime with `reset()`, however, this is an anti-pattern, as it is prone to mistakes. It's much safer to just initialize a new chat.
chat . reset ( ) ;
const article = await chat . request (
prompt . text ( 'Write a blog post about the financial crisis of 2008' ) ,
) ;
const entities = await chat . request (
prompt . json ( {
message :
'What are the different entities in the above blog post? Respond in a JSON array, where the items in the array are just the names of the entities.' ,
schema : z . array ( z . string ( ) ) ,
} ) ,
) ;
const titles = await chat . request (
prompt . bulletPoints ( {
message : 'Write a good title for this post' ,
amount : 10 ,
} ) ,
) ; ข้อผิดพลาดทั่วไปกับ LLM APIs คือการใช้โทเค็น - คุณได้รับอนุญาตให้พอดีกับข้อมูลจำนวนหนึ่งในหน้าต่างบริบท retainMemory กรณีของ Llamaflow ซึ่ง true ว่าคุณถูก จำกัด จำนวนข้อความทั้งหมดที่คุณสามารถส่งได้
Llamaflow จะพิจารณาโดยอัตโนมัติว่าคำขอจะละเมิดขีด จำกัด โทเค็นก่อนที่จะส่งคำขอจริงไปยังผู้ให้บริการรุ่น (เช่น OpenAI) สิ่งนี้จะบันทึกการโทรไปกลับเครือข่ายหนึ่งครั้งและให้คุณจัดการกับข้อผิดพลาดประเภทนี้ในลักษณะที่ตอบสนอง วิธีทั่วไปในการจัดการข้อผิดพลาดเหล่านี้คือการลบข้อความในประวัติข้อความ (หากคุณใช้แชทกับชุด retainMemory ) หรือแบ่งเนื้อหาของคุณออกเป็นกลุ่มที่เล็กลงและประมวลผลในหลายคำขอ
นี่คือตัวอย่างของการจับข้อผิดพลาดโทเค็นล้น โปรดทราบว่ามีการตั้ง minimumResponseTokens เป็นค่าสูงเพื่อทริกเกอร์ข้อผิดพลาดนี้อย่างชัดเจน ( gpt-3.5-turbo มีขีด จำกัด บริบทสูงสุดที่ 4096 ดังนั้นการตั้งค่าขีด จำกัด ขั้นต่ำเป็น 4095 หมายความว่ามีเพียง 1 โทเค็นที่เหลือสำหรับพรอมต์จริงซึ่งไม่เพียงพอสำหรับตัวอย่างด้านล่าง)
try {
// make sure to set the `contextSize` to enable automatic token checking
const model = new OpenAI (
{ apiKey : 'YOUR_OPENAI_KEY' } ,
{ model : 'gpt-3.5-turbo' , contextSize : 4096 } ,
) ;
const chat = model . chat ( {
systemMessage : 'You are an AI assistant' ,
} ) ;
await chat . request (
{ message : 'hello world, testing overflow logic' } ,
{ minimumResponseTokens : 4095 } ,
) ;
} catch ( e ) {
if ( e instanceof TokenError ) {
console . info (
`Caught token overflow, overflowed tokens: ${ e . overflowTokens } ` ,
) ;
}
} วิธีทั่วไปในการจัดการปัญหาการ จำกัด โทเค็นคือการแยกเนื้อหาของคุณ Llamaflow ให้วิธีการตัวช่วยที่มีประโยชน์ซึ่งห่อเมธอด request chat.request และจะแยกข้อความของคุณโดยอัตโนมัติตามการกำหนดค่าก้อนอินพุต มันฉลาดพอที่จะแยกข้อความของคุณเฉพาะเมื่อกำหนดว่ามันอยู่เหนือขีด จำกัด โทเค็นและจะพยายามรักษาข้อความต้นฉบับให้มากที่สุดเท่าที่จะทำได้
const response = await chat . requestWithSplit (
'hello world, testing overflow logic' ,
( text ) =>
prompt . text ( {
message : `Add other required prompts first, then add your content: ${ text } ` ,
} ) ,
) ; โปรดทราบว่าตอนนี้เนื้อหาหลักของพรอมต์จะถูกส่งก่อน นี่คือเนื้อหาที่จะถูกแยกด้วยตัวแยกข้อความ (ตาม n . , และ ตัวละครก่อนเพื่อพิมพ์) คุณสามารถเพิ่มพรอมต์ที่จำเป็นเพิ่มเติมและรวมเข้ากับพรอมต์เนื้อหาในพารามิเตอร์ responseFn
Llamaflow Usese โมดูล debug สำหรับข้อความบันทึกและข้อผิดพลาด หากต้องการเรียกใช้ในโหมดดีบักให้ตั้งค่าตัวแปร DEBUG Env:
DEBUG=llamaflow:* yarn playground
นอกจากนี้คุณยังสามารถระบุประเภทการบันทึกที่แตกต่างกันผ่าน:
DEBUG=llamaflow:error yarn playground DEBUG=llamaflow:log yarn playground
Llamaflow ยังมาพร้อมกับการสนับสนุนสำหรับโมเดล Openai ของ Azure รุ่น Azure มักจะเร็วกว่าและเชื่อถือได้มากกว่าจุดสิ้นสุด API ของ OpenAI ในการใช้จุดสิ้นสุดของ Azure คุณต้องรวมตัวเลือกเฉพาะ Azure 2 ตัวเลือกเมื่อเริ่มต้นโมเดล OpenAI, azureDeployment และ azureEndpoint ตอนนี้ฟิลด์ apiKey จะถูกใช้สำหรับคีย์ Azure API
คุณสามารถค้นหาคีย์ Azure API และจุดสิ้นสุดในพอร์ทัล Azure การปรับใช้ Azure จะต้องถูกสร้างขึ้นภายใต้พอร์ทัล Azure AI
โปรดทราบว่าพารามิเตอร์ model ใน ModelConfig จะถูกละเว้นเมื่อใช้ Azure นี่เป็นเพราะในระบบ Azure model จะถูกเลือกจากการสร้างการปรับใช้ไม่ใช่เวลาทำงาน
const model = new OpenAI ( {
apiKey : 'AZURE_OPENAI_KEY' ,
azureDeployment : 'AZURE_DEPLOYMENT_NAME' ,
azureEndpoint : 'AZURE_ENDPOINT' ,
} ) ; รุ่น Llamaflow รุ่นเดียวที่รองรับปัจจุบันคือโมเดลการแชทของ OpenAI
const model = new OpenAI ( openAiConfig , modelConfig ) ; interface OpenAIConfig {
apiKey : string ;
} แผนที่กำหนดค่าเหล่านี้ไปยังการกำหนดค่าของ OpenAI โดยตรงดูเอกสาร: https://platform.openai.com/docs/api-reference/chat/Create
interface ModelConfig {
model ?: string ;
maxTokens ?: number ;
temperature ?: number ;
topP ?: number ;
stop ?: string | string [ ] ;
presencePenalty ?: number ;
frequencyPenalty ?: number ;
logitBias ?: Record < string , number > ;
user ?: string ;
stream ?: boolean ;
} เมื่อ stream ถูกตั้งค่าเป็น true คุณสามารถเข้าถึงเอาต์พุตบางส่วนของคำขอของโมเดลโดยส่งผ่านตัวส่งสัญญาณไปยัง ChatRequestOptions เมื่อทำการร้องขอ เอาต์พุตบางส่วนจะถูกส่งเป็นสตริงผ่านเหตุการณ์ data
ในการร้องขอโมเดลคุณต้องสร้างวัตถุพรอมต์ก่อน พรอมต์ให้วิธีการเพิ่มการตรวจสอบและการลองตรรกะอีกครั้งในแต่ละคำขอ
import { prompt } from 'llama-flow' ;
prompt . text ( prompt : string ) ;
prompt . text ( prompt : RawPrompt ) ;
prompt . json ( prompt : JSONPrompt ) ;
prompt . bulletPoints ( prompt : BulletPointsPrompt ) ;
prompt . boolean ( prompt : BooleanPrompt ) ; คุณสามารถขอเป็นสตริงหรือเป็น RawPrompt
interface RawPrompt < T = string > {
message : string ;
parse ?: (
response : ChatResponse < string > ,
) => MaybePromise <
{ success : false ; retryPrompt ?: string } | { success : true ; data : T }
> ;
promptRetries ?: number ;
}ข้อความ นี่คือข้อความที่ส่งไปยังโมเดล
แยกวิเคราะห์ คุณสามารถใช้ตัวแยกวิเคราะห์แบบกำหนดเองได้โดยกำหนดวิธี parse ของคุณเอง
เมื่อกำหนดวิธี parse แบบกำหนดเองที่ส่งคืนประเภทข้อมูลที่กำหนดเองคุณสามารถเพิ่มประเภททั่วไปลงใน RawPrompt ซึ่งจะส่งการแยกประเภทของ parse ไปยังทั่วไปโดยอัตโนมัติ นอกจากนี้ยังจะเผยแพร่ประเภทไปตลอดทางผ่านวิธี chat.request
หากข้อมูลที่ส่งคืนโดยโมเดลนั้นไม่ถูกต้องคุณสามารถส่งคืนสตริง retryPrompt แบบกำหนดเองซึ่งจะทำให้ Llamaflow กลับมาทำซ้ำ
PrompTretries กำหนดจำนวนครั้งที่จะ reisk โมเดลก่อนที่คำขอจะส่งข้อผิดพลาด ค่าเริ่มต้นเป็น 3. โปรดทราบว่า parse จะต้องส่งคืน retryPrompt ที่ถูกต้องสำหรับการลองใหม่ใด ๆ ที่จะพยายาม
interface BooleanPrompt {
message : string ;
promptRetries ?: number ;
} ใช้พรอมต์นี้หากคุณต้องการถามแบบจำลองคำถามที่คุณคาดหวังการตอบกลับ true หรือ false เท่านั้น
ส่งข้อความ ค้นหาเพื่อส่งไปยังรุ่น พรอมต์นี้จะผนวกคำแนะนำการจัดรูปแบบโดยอัตโนมัติไปยังข้อความที่ส่งไปยังโมเดลที่บอกให้โมเดลจัดรูปแบบการตอบสนองเป็นบูลีนดังนั้นคุณสามารถรวมข้อความค้นหาใน message โดยไม่ต้องเขียนคำสั่งการจัดรูปแบบเพิ่มเติมใด ๆ
interface BulletPointsPrompt {
message : string ;
amount ?: number ;
length ?: number ;
promptRetries ?: number ;
}ใช้พรอมต์นี้หากคุณต้องการให้โมเดลส่งคืนรายการสตริง
ส่งข้อความ ค้นหาเพื่อส่งไปยังรุ่น พรอมต์นี้จะผนวกคำแนะนำการจัดรูปแบบโดยอัตโนมัติกับข้อความที่บอกโมเดลวิธีการจัดรูปแบบการตอบสนอง
จำนวน จำนวนสัญลักษณ์แสดงหัวข้อย่อยที่ควรส่งคืน
ความยาว จำนวนอักขระสูงสุดที่ควรอยู่ในแต่ละจุดกระสุน
interface JSONPrompt < T extends z . ZodType > {
message : string ;
schema : T ;
parseResponse ?: ( res : string ) => MaybePromise < z . infer < T > > ;
retryMessage ?: string ;
promptRetries ?: number ;
}ข้อความ ข้อความเพื่อส่งไปยังรุ่น ซึ่งแตกต่างจาก Boolean หรือ Bullet Point Propts พรอมต์นี้ไม่ได้สร้างคำแนะนำในการสร้างแบบจำลองโดยอัตโนมัติ ดังนั้นในฐานะที่เป็นส่วนหนึ่งของข้อความของคุณไปยังรุ่นคุณควรรวมคำแนะนำในการจัดรูปแบบเพื่อส่งคืนข้อมูลในรูปแบบ JSON รวมถึงรูปร่างของ JSON
Schema นี่คือ zod schema ที่จะใช้ในการแยกวิเคราะห์และ typecast การตอบสนองจากโมเดล
ParserEsponse หากคุณขอให้โมเดล ไม่ ส่งคืนข้อมูลในรูปแบบ JSON คุณสามารถกำหนดตัวแยกวิเคราะห์แบบกำหนดเองเพื่อแยกสตริงส่งคืนลงใน JSON ก่อนที่จะส่งไปยัง schema เพื่อตรวจสอบความถูกต้อง
retrymessage หากการแยกวิเคราะห์สคีมาล้มเหลวสิ่งนี้จะถูกใช้เป็นส่วนหนึ่งของข้อความที่ส่งไปยังโมเดลเพื่อ reask สำหรับการตอบกลับที่จัดรูปแบบอย่างถูกต้อง โปรดทราบว่าพรอมต์นี้จะสร้างข้อความ Reask โดยอัตโนมัติขึ้นอยู่กับข้อผิดพลาดในการแยกวิเคราะห์สคีมา (เช่นหากคีย์เฉพาะหายไป LlamaFlow จะขอให้โมเดลรวมคีย์เฉพาะนั้น) ดังนั้นฟิลด์นี้ล้วนเพื่อให้บริบทเพิ่มเติมกับโมเดลบน Reask
วัตถุแชทเก็บเซสชันการแชทกับรุ่น เซสชั่นจะดูแลการจัดเก็บประวัติข้อความดังนั้นคุณสามารถพูดคุยกับโมเดลต่อไปได้โดยทำคำขออื่น
const chat = model . chat ( config : ChatConfig ) ;ตัวเลือก คุณสามารถตั้งค่าพฤติกรรมการเก็บรักษาหน่วยความจำรวมถึงตัวเลือกคำขอเริ่มต้นสำหรับทุกคำขอที่ส่งในแชทนี้
export interface ChatConfig {
// the message injected at the start of every chat to steer the agent
systemMessage : string ;
// if chat memory should be retained after every request. when enabled, the chat's behavior will be similar to a normal user chat room, and model can have access to history when making inferences. defaults to false
retainMemory ?: boolean ;
// set default request options. note that this can be overridden on a per-request basis
options ?: ChatRequestOptions ;
} เพื่อส่งคำขอไปยังเซสชันแชท:
const res : ChatResponse = await chat . request ( prompt , options : ChatRequestOptions ) ;ตัวเลือก คุณสามารถแทนที่ตัวเลือกคำขอเริ่มต้นผ่านพารามิเตอร์นี้ คำขอจะถูกลองใหม่โดยอัตโนมัติหากมีข้อผิดพลาด Ratelimit หรือ Server
โปรดทราบว่าการลองใหม่ในคำขอจะไม่นับรวมไปยัง reask พร้อมท์ที่กำหนดไว้ในส่วนพรอมต์ด้านบน
type ChatRequestOptions = {
// the number of time to retry this request due to rate limit or recoverable API errors
retries ?: number ;
retryInterval ?: number ;
timeout ?: number ;
// the minimum amount of tokens to allocate for the response. if the request is predicted to not have enough tokens, it will automatically throw a 'TokenError' without sending the request
minimumResponseTokens ?: number ;
// override the messages used for completion, only use this if you understand the API well
messages ?: Message [ ] ;
// pass in an event emitter to receive message stream events
events ?: EventEmitter ;
} ; การตอบสนองการแชทอยู่ในรูปแบบต่อไปนี้:
interface ChatResponse < T = string > {
content : T ;
model : string ;
// set to true if this content was streamed. note to actually access the stream, you have to pass in an event emitter via ChatRequestOptions
isStream : boolean ;
usage : {
promptTokens : number ;
completionTokens : number ;
totalTokens : number ;
} ;
}เนื้อหา ที่แยกวิเคราะห์และพิมพ์เนื้อหาจากพรอมต์ ประเภทจะถูกตั้งค่าโดยอัตโนมัติขึ้นอยู่กับพรอมต์ที่คุณใช้
จำลอง โมเดลเฉพาะที่ใช้สำหรับการเสร็จสิ้น (เช่น gpt-3.5-turbo-0301 )
การใช้ ข้อมูลการใช้โทเค็นแผนที่นี้การตอบสนองการใช้งานของ OpenAI โดยตรง
หากคุณต้องการรีเซ็ตประวัติข้อความในประวัติการแชทมีวิธีการช่วยง่าย ๆ :
chat . reset ( ) ;โปรดทราบว่าวิธีนี้เป็นฟักหลบหนี จะเป็นการดีกว่าที่จะเพียงแค่ตั้งอินสแตนซ์เซสชันการแชทใหม่หากคุณต้องการทำคำขอใหม่ด้วยกระดานชนวนที่สะอาด ตรรกะที่ซับซ้อนซึ่งคุณกำลังรีเซ็ตเซสชันการแชทหลายครั้งอาจเป็นเรื่องยากที่จะติดตามและยากที่จะแก้ไขข้อบกพร่อง
โปรดทราบว่าหากคุณต้องการหลีกเลี่ยงตรรกะการจัดการแชทของ Llamaflow และส่งคำขอไปยังรุ่น underlaying โดยตรงคุณสามารถส่งคำขอไปยังโมเดลโดยตรงโดยไม่ต้องแชทอินสแตนซ์:
const model = new OpenAI ( openAiConfig , modelConfig ) ;
const res = await model . request ( messages : Message [ ] , options : ChatRequestOptions ) ; สิ่งนี้จะหลีกเลี่ยงการจัดการประวัติการแชทการจัดรูปแบบและการแยกวิเคราะห์รวมถึงตรรกะของบุคคล คุณยังสามารถใช้ประโยชน์จากฟีเจอร์ API retries ผ่าน ChatRequestOptions