
驚異的なAIは、大規模な言語モデル向けのターンベースの会話プロンプトジェネレーターを構築するためのタイプスクリプトパッケージです。このパッケージは、俳優と呼ばれる複数のエンティティ間の会話型相互作用をシミュレートするためのビルディングブロックを提供します。アクターは情報を共有し、対話に従事し、事前定義されたルールに基づいて会話のコースを動的に調整できます。
mustache )が1つしかないため、軽量で使いやすくなります。npm経由でパッケージをインストールできます。
npm install @wecandobetter/phenomenal-aiまたは糸経由:
yarn add @wecandobetter/phenomenal-aiConversationクラスはすべての俳優をカプセル化し、俳優のターンの歴史、コンテキスト、スケジューリングを管理します。 Actorクラスは、会話の中で俳優を表しています。
次の例はConversationクラスを使用して2人の俳優間の会話をシミュレートする方法を示しています。
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ライセンスの下でライセンスされています。あなた自身のプロジェクトに自由に使用してください。
ハッピーコーディング! ?
問題が発生したり、改善の提案がある場合は、お気軽に問題やプルリクエストを開いてください。
あなたがこのプロジェクトが好きなら§を贈りましょう!
私たちがより良くすることができることによって❤️でコード化されています。