
现象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许可获得许可。随时将其用于您自己的项目。
愉快的编码! ?
如果您遇到任何问题或提出改进的建议,请随时打开问题或提取请求。
如果您喜欢这个项目,请给!
用❤️编码的我们可以做得更好。