
Phelomenal AI عبارة عن حزمة TypeScript لإنشاء مولد موجه للمحادثة القائم على الدوران لنماذج اللغة الكبيرة. توفر هذه الحزمة اللبنات الأساسية لمحاكاة التفاعلات المحادثة بين كيانات متعددة ، ويشار إليها باسم الجهات الفاعلة. يمكن للممثلين مشاركة المعلومات ، والمشاركة في الحوار ، وضبط مسار المحادثة بناءً على القواعد المحددة مسبقًا.
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 })مولد غير متزامن يعطي السماعة والنص واختياريًا تدمسين كل منعطف في المحادثة.
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 History تاريخ المحادثة.
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 للممثل.
طلبات السحب موضع ترحيب. للتغييرات الرئيسية ، يرجى فتح مشكلة أولاً لمناقشة ما تريد تغييره. لا تتردد في التحقق من صفحة المشكلات.
هذا المشروع مرخص بموجب ترخيص معهد ماساتشوستس للتكنولوجيا. لا تتردد في استخدامه لمشاريعك الخاصة.
ترميز سعيد! ؟
إذا واجهت أي مشاكل أو لديك اقتراحات للتحسينات ، فلا تتردد في فتح مشكلة أو طلب سحب.
إعطاء ️ إذا كنت تحب هذا المشروع!
مشفرة مع ❤ من قبل يمكننا أن نفعل أفضل.