Spaces:
Sleeping
Sleeping
File size: 3,217 Bytes
30f1ff2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import LLM from "./llm";
export class ConversationLLM {
constructor(character1Name, character2Name, character1Prompt, character2Prompt, situation_prompt, outputFormatPrompt, functionDescriptions, functionPrompt) {
this.character1Name = character1Name;
this.character2Name = character2Name;
this.character1Prompt = character1Prompt;
this.character2Prompt = character2Prompt;
this.situation_prompt = situation_prompt;
this.outputFormatPrompt = outputFormatPrompt;
this.functionDescriptions = functionDescriptions;
this.functionPrompt = functionPrompt;
}
async generateConversation(numTurns = 3) {
try {
let conversation = [];
const llm = new LLM();
for (let i = 0; i < numTurns; i++) {
// Alternate between characters for each turn
const isCharacter1Turn = i % 2 === 0;
const currentSpeaker = isCharacter1Turn ? this.character1Prompt : this.character2Prompt;
const currentListener = isCharacter1Turn ? this.character2Prompt : this.character1Prompt;
const currentSpeakerName = isCharacter1Turn ? this.character1Name : this.character2Name;
const currentListenerName = isCharacter1Turn ? this.character2Name : this.character1Name;
// Format the conversation history as a proper chat message array
const conversationHistory = [...conversation];
// Create system message for current speaker
const systemMessage = {
role: 'system',
content: `${this.situation_prompt}\nRoleplay as: ${currentSpeakerName}\nMake only the response to the user. Only speech, no speech style. You have the following personality: ${currentSpeaker}. You talk to ${currentListenerName}.`
};
// Get response from LLM with proper message format
const response = await llm.getChatCompletion(
systemMessage.content,
conversationHistory.length > 0
? JSON.stringify(conversationHistory)
: "Start the conversation"
);
// Ensure the response is in the correct format with the proper character role
const parsedResponse = {
role: currentSpeakerName, // Use the character name instead of prompt
content: this.parseConversation(response)
};
conversation.push(parsedResponse);
}
const analysis = await llm.getFunctionKey(
this.functionDescriptions,
this.functionPrompt + JSON.stringify(conversation)
);
return {
conversation,
analysis
};
} catch (error) {
console.error('Error generating conversation:', error);
throw error;
}
}
parseConversation(llmResponse) {
return llmResponse;
}
}
|