
現象AI是用於為大型語言模型構建基於轉彎的對話提示生成器的打字條包。該軟件包提供了模擬多個實體之間的對話互動的構建塊,稱為參與者。演員可以根據預定義的規則共享信息,進行對話並動態調整對話。
mustache ),使其輕巧且易於使用。您可以通過npm安裝軟件包:
npm install @wecandobetter/phenomenal-ai或通過紗線:
yarn add @wecandobetter/phenomenal-aiConversation類封裝了所有演員,並管理演員回合的歷史,上下文和安排。 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類代表對話的歷史。
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許可獲得許可。隨時將其用於您自己的項目。
愉快的編碼! ?
如果您遇到任何問題或提出改進的建議,請隨時打開問題或提取請求。
如果您喜歡這個項目,請給!
用❤️編碼的我們可以做得更好。