
ปรากฎการณ์ AI เป็นแพ็คเกจ typenscript สำหรับการสร้างเครื่องกำเนิดพรอมต์การสนทนาแบบเทิร์นสำหรับแบบจำลองภาษาขนาดใหญ่ แพ็คเกจนี้ให้หน่วยการสร้างสำหรับการจำลองปฏิสัมพันธ์ระหว่างการสนทนาระหว่างหลายหน่วยงานเรียกว่านักแสดง นักแสดงสามารถแบ่งปันข้อมูลมีส่วนร่วมในการสนทนาและปรับหลักสูตรการสนทนาตามกฎที่กำหนดไว้ล่วงหน้า
mustache ) ทำให้มีน้ำหนักเบาและใช้งานง่าย คุณสามารถติดตั้งแพ็คเกจผ่าน npm :
npm install @wecandobetter/phenomenal-aiหรือผ่านเส้นด้าย:
yarn add @wecandobetter/phenomenal-ai ชั้นเรียน Conversation สรุปนักแสดงและจัดการประวัติศาสตร์บริบทและการกำหนดเวลาของการผลัดกันของนักแสดง คลาส Actor เป็นตัวแทนของนักแสดงในการสนทนา
ตัวอย่างต่อไปนี้แสดงวิธีใช้คลาส Conversation เพื่อจำลองการสนทนาระหว่างนักแสดงสองคน
import { Actor , Conversation } from "@wecandobetter/phenomenal-ai" ;
import { generateText } from "./text-generation" ; // provide your own text generation function
// Define your actors
const actors = [
new Actor ( "John" ) ,
new Actor ( "Emma" ) ,
] ;
// Initialize a conversation
const conversation = new Conversation ( "Morning Talk" , { actors } ) ;
// Add a context entry
conversation . context . set ( "topic" , "Current topic of conversation" , "AI Ethics" ) ;
// Make a query:
const response = await conversation . query ( {
speaker : actors [ 0 ] , // the speaker, i.e. the actor asking the question
answerer : actors [ 1 ] , // the answerer, i.e. the actor answering the question
query : "What is the topic of conversation?" , // the query to be answered
generateText , // provide your own text generation function
store : true , // store the response in the history
} ) ;
console . log ( ` ${ response . speaker } : ${ response . text } ` ) ;
// Moderate the conversation by injecting a new message:
conversation . inject ( "Let's get back to the topic of conversation." , {
speaker : "Moderator" , // the speaker of the message (defaults to `System`)
ephemeral : true , // this message will not be stored in the history
} ) ;
// Make one turn:
const turn = await conversation . turn ( {
actor : conversation . scheduler . getNextSpeaker ( ) , // get the next speaker from the scheduler
generateText , // provide your own text generation function
} ) ;
console . log ( ` ${ turn . speaker } : ${ turn . text } ` ) ;
// or use an async generator:
const ac = new AbortController ( ) ;
const loop = conversation . loop ( {
signal : ac . signal , // provide an AbortSignal to stop the loop
generateText , // provide your own text generation function
scheduler : conversation . scheduler , // provide your own scheduler
} ) ;
for await ( const turn of loop ) {
console . log ( ` ${ turn . speaker } : ${ turn . text } ` ) ;
} คลาสหลักของแพ็คเกจนี้คือ Conversation ConversationHistory และ Actor
คลาส Conversation แสดงถึงการสนทนาอย่างต่อเนื่องระหว่างนักแสดงหลายคน
id : ตัวระบุที่ไม่ซ้ำกันสำหรับการสนทนาname : ชื่อของการสนทนาactors : อาเรย์ของ Actor ที่เข้าร่วมในการสนทนาhistory : ประวัติความเป็นมาของการสนทนาcontext : บริบทที่ใช้ร่วมกันระหว่างนักแสดงทุกคนในการสนทนา นี่คือวิธีการที่มีอยู่ในคลาส Conversation
new Conversation(name: string, { actors: Actor[], generateText?: GenerateText, scheduler?: Scheduler, messages?: Message[], windowss?: { history?: number } }) เริ่มต้นอินสแตนซ์ใหม่ของคลาส Conversation
const conversation = new Conversation ( "Morning Talk" , {
actors : [
new Actor ( "John" ) ,
new Actor ( "Emma" ) ,
] ,
generateText : generateText , // provide your own text generation function
scheduler : RoundRobinScheduler , // provide your own scheduler
messages : [ ] , // bootstrap the conversation with messages
windows : { // configure the conversation window
max : 1024 , // the maximum number of tokens to unmask in the history
} ,
} ) ; conversation.inject(text: string, { speaker = "System" embeddings?: number[][], ephemeral?: true })ฉีดข้อความใหม่ลงในการสนทนา ส่งคืนข้อความที่ฉีด
const message = conversation . inject (
"Let's get back to the topic of conversation." ,
{
speaker : "Moderator" ,
ephemeral : true , // if true, the message will not be stored in the history after the next turn
} ,
) ; conversation.query({ speaker: Actor, answerer: Actor, query: string, generateText?: GenerateText, store = false })ส่งคืนสัญญาที่แก้ไขเพื่อตอบกลับ
const response = await conversation . query ( {
speaker : actors [ 0 ] , // the speaker, i.e. the actor asking the question
answerer : actors [ 1 ] , // the answerer, i.e. the actor answering the question
query : "What is the topic of conversation?" , // the query to be answered
generateText , // provide your own text generation function
store : true , // store the response in the history
} ) ;
console . log ( ` ${ response . speaker } : ${ response . text } ` ) ; conversation.turn({ actor: Actor, generateText?: GenerateText })ส่งคืนสัญญาที่แก้ไขเพื่อตอบกลับ
const response = await conversation . turn ( {
actor : conversation . scheduler . getNextSpeaker ( ) , // get the next speaker from the scheduler
generateText , // provide your own text generation function
} ) ;
console . log ( ` ${ response . speaker } : ${ response . text } ` ) ; conversation.loop({ signal: AbortSignal; generateText?: GenerateText })เครื่องกำเนิด Async ที่ให้ลำโพงข้อความและตัวเลือกการฝังตัวของแต่ละเทิร์นในการสนทนา
const ac = new AbortController ( ) ;
// start the loop, which will yield the responses
const loop = conversation . loop ( {
signal : ac . signal , // provide an AbortSignal to stop the loop
generateText , // provide your own text generation function
} ) ;
for await ( const response of loop ) {
console . log ( ` ${ response . speaker } : ${ response . text } ` ) ;
} conversation.toJSON()ส่งคืนตัวแทน JSON ของการสนทนา
const json = conversation . toJSON ( ) ; คลาส ConversationHistory แสดงถึงประวัติของการสนทนา
messages : อาร์เรย์ของข้อความในการสนทนา นี่คือวิธีการที่มีอยู่ในชั้นเรียน ConversationHistory
new ConversationHistory(messages?: Message[]) เริ่มต้นอินสแตนซ์ใหม่ของคลาส ConversationHistory
const history = new ConversationHistory ( [
{ speaker : "John" , text : "Hello, Emma!" } ,
{ speaker : "Emma" , text : "Hi, John!" } ,
] ) ; history.push(message: PartialBy<Message, "feedback">)ผลักข้อความใหม่ไปยังประวัติศาสตร์
history . push ( { actor : "John" , text : "Hello, Emma!" } ) ; history.getMessagesFor(actor: string)ส่งคืนแผนที่ของดัชนีและข้อความสำหรับนักแสดงที่กำหนด
const messages = history . getMessagesFor ( "John" ) ; history.getStats()ส่งคืนสถิติเกี่ยวกับประวัติ สถิติจัดกลุ่มโดยนักแสดง
const stats = history . getStats ( ) ;
console . log ( stats ) ;
// {
// "Bob": {
// count: 2, // total number of messages
// textCount: 22, // total number of characters
// percentage: 0.5, // percentage of messages in the conversation
// textPercentage: 0.5, // percentage of characters in the conversation
// },
// } history.cleanEphemeral()ลบข้อความชั่วคราวจากประวัติศาสตร์
history . cleanEphemeral ( ) ; history.up(message: Message)เพิ่มข้อเสนอแนะในเชิงบวกให้กับข้อความที่กำหนด
const message = history . messages [ 0 ] ; // get message from somewhere
history . up ( message ) ; history.down(message: Message)เพิ่มข้อเสนอแนะเชิงลบให้กับข้อความที่กำหนด
const message = history . messages [ 0 ] ; // get message from somewhere
history . down ( message ) ; history.first(): Messageส่งคืนข้อความแรกในประวัติศาสตร์
history.last(): Messageส่งคืนข้อความสุดท้ายในประวัติศาสตร์
history.toJSON()ส่งคืนตัวแทน JSON ของประวัติศาสตร์
history.clear()ล้างประวัติศาสตร์
คลาส Actor แสดงถึงนิติบุคคลที่มีส่วนร่วมในการสนทนา
id : ตัวระบุที่ไม่ซ้ำกันสำหรับนักแสดงname : ชื่อของนักแสดงtemplate : เทมเพลตสำหรับพรอมต์ของนักแสดงcontext : บริบทที่ใช้ร่วมกันระหว่างนักแสดงทุกคนในการสนทนาpersona : บุคคลของนักแสดงknowledge : ความรู้เกี่ยวกับนักแสดงmemory : ความทรงจำของนักแสดง นี่คือวิธีการที่มีอยู่ในคลาส Actor
new Actor(name: string, { template?: string, persona?: Persona, knowledge?: Knowledge, memory?: Memory }) เริ่มต้นอินสแตนซ์ใหม่ของคลาส Actor หากไม่มีเทมเพลตจะใช้เทมเพลตเริ่มต้น
const actor = new Actor ( "John" , {
template : "Hello, my name is {{name}}." ,
) ; actor.render(conversation: Conversation)แสดงเทมเพลตของนักแสดงเป็นพรอมต์ พรอมต์เป็นข้อความที่สามารถใช้งานโดยรูปแบบภาษาขนาดใหญ่ที่คุณเลือกเพื่อสร้างการตอบกลับ
const prompt = actor . render ( conversation ) ; actor.toJSON()ส่งคืนตัวแทน JSON ของนักแสดง
ยินดีต้อนรับคำขอดึง สำหรับการเปลี่ยนแปลงครั้งใหญ่โปรดเปิดปัญหาก่อนเพื่อหารือเกี่ยวกับสิ่งที่คุณต้องการเปลี่ยนแปลง อย่าลังเลที่จะตรวจสอบหน้าปัญหา
โครงการนี้ได้รับใบอนุญาตภายใต้ใบอนุญาต MIT อย่าลังเลที่จะใช้สำหรับโครงการของคุณเอง
การเข้ารหัสมีความสุข! -
หากคุณพบปัญหาใด ๆ หรือมีข้อเสนอแนะสำหรับการปรับปรุงอย่าลังเลที่จะเปิดปัญหาหรือคำขอดึง
ให้️ถ้าคุณชอบโครงการนี้!
เขียนรหัสด้วย❤โดยเราสามารถทำได้ดีกว่า