Skip to content

Commit

Permalink
- adjust getFormatedMessages
Browse files Browse the repository at this point in the history
- adjust user_prompt in chains
- adjust lint fixed
  • Loading branch information
Joao Paulo Nobrega committed Feb 22, 2024
1 parent aac1293 commit 1487e98
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class Agent extends AgentBaseCommand implements IAgent {
query: question,
question: question,
chat_history: chatMessages,
format_chat_messages: await chatHistory.getFormatedMessages(),
format_chat_messages: chatHistory.getFormatedMessages(chatMessages),
user_prompt: this._settings.systemMesssage,
});

Expand Down
44 changes: 24 additions & 20 deletions src/services/chain/openapi-base-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ export class OpenApiBaseChain extends BaseChain {
readonly inputKey = 'query';
readonly outputKey = 'openAPIResult';
private _input: OpenApiBaseChainInput;
private _logger: Console;

constructor(input: OpenApiBaseChainInput) {
super(input);
this._input = input;
this._logger = console;
}

get inputKeys(): string[] {
Expand All @@ -38,21 +40,23 @@ export class OpenApiBaseChain extends BaseChain {
}

private getOpenApiPrompt(): string {
return `You are an AI with expertise in OpenAPI and Swagger.\n
You should follow the following rules when generating and answer:\n
- Only execute the request on the service if the question is not in CHAT HISTORY, if the question has already been answered, use the same answer and do not make a request on the service.
- Only attempt to answer if a question was posed.\n
- Always answer the question in the language in which the question was asked.\n\n
-------------------------------------------
USER PROMPT: ${this._input.customizeSystemMessage || ''}
-------------------------------------------\n
SCHEMA: {schema}\n
-------------------------------------------\n
CHAT HISTORY: {format_chat_messages}\n
-------------------------------------------\n
QUESTION: {question}\n
------------------------------------------\n
API ANSWER:`;
return `
You are an AI with expertise in OpenAPI and Swagger.\n
You should follow the following rules when generating and answer:\n
- Only execute the request on the service if the question is not in CHAT HISTORY, if the question has already been answered, use the same answer and do not make a request on the service.
- Only attempt to answer if a question was posed.\n
- Always answer the question in the language in which the question was asked.\n\n
-------------------------------------------\n
USER PROMPT: {user_prompt}\n
-------------------------------------------\n
SCHEMA: {schema}\n
-------------------------------------------\n
CHAT HISTORY: {format_chat_messages}\n
-------------------------------------------\n
QUESTION: {question}\n
------------------------------------------\n
API ANSWER:
`;
}

private buildPromptTemplate(systemMessages: string): BasePromptTemplate {
Expand Down Expand Up @@ -83,13 +87,12 @@ export class OpenApiBaseChain extends BaseChain {
return text;
}


async _call(
values: ChainValues,
runManager?: CallbackManagerForChainRun
): Promise<ChainValues> {
console.log('Values: ', values);
console.log('OPENAPI Input: ', values[this.inputKey]);
this._logger.log('Values: ', values);
this._logger.log('OPENAPI Input: ', values[this.inputKey]);

const question = values[this.inputKey];
const schema = this._input.spec;
Expand All @@ -109,13 +112,14 @@ export class OpenApiBaseChain extends BaseChain {
schema,
chat_history: values?.chat_history,
format_chat_messages: values?.format_chat_messages,
user_prompt: this._input.customizeSystemMessage || '',
});

console.log('OPENAPI Resposta: ', answer);
this._logger.log('OPENAPI Resposta: ', answer);

answer = rs?.response;
} catch (error) {
console.error('OPENAPI Error: ', error);
this._logger.error('OPENAPI Error: ', error);

answer = this.tryParseText(error?.message);
} finally {
Expand Down
5 changes: 3 additions & 2 deletions src/services/chain/sql-database-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ export default class SqlDatabaseChain extends BaseChain {
Your response must only be a valid SQL query, based on the schema provided.\n
-------------------------------------------\n
Here are some important observations for generating the query:\n
- Only execute the request on the service if the question is not in CHAT HISTORY, if the question has already been answered, use the same answer and do not make a query on the database.
${this.customMessage}\n
- Only execute the request on the service if the question is not in CHAT HISTORY, if the question has already been answered, use the same answer and do not make a query on the database.\n
{user_prompt}\n
-------------------------------------------\n
SCHEMA: {schema}\n
-------------------------------------------\n
Expand Down Expand Up @@ -174,6 +174,7 @@ export default class SqlDatabaseChain extends BaseChain {
question: (input: { question: string }) => input.question,
chat_history: () => values?.chat_history,
format_chat_messages: () => values?.format_chat_messages,
user_prompt: () => this.customMessage,
},
this.buildPromptTemplate(this.getSQLPrompt()),
this.llm.bind({ stop: ['\nSQLResult:'] }),
Expand Down
2 changes: 1 addition & 1 deletion src/services/chat-history/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface IChatHistory {
addUserMessage(message: string): Promise<void>;
addAIChatMessage(message: string): Promise<void>;
getMessages(): Promise<BaseMessage[]>;
getFormatedMessages(): Promise<string>;
getFormatedMessages(messages: BaseMessage[]): string;
clear(): Promise<void>;
getChatHistory(): BaseChatMessageHistory;
getBufferMemory(): BufferMemory;
Expand Down
6 changes: 2 additions & 4 deletions src/services/chat-history/memory-chat-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ class MemoryChatHistory implements IChatHistory {
return this._history?.getMessages();
}

async getFormatedMessages(): Promise<string> {
const messages = await this._history?.getMessages();

getFormatedMessages(messages: BaseMessage[]): string {
const cut = messages
.slice(-(this._settings?.limit || 5));

const formated = cut.map((message) => `${message._getType().toUpperCase()}: ${message.content}`).join('\n');

return formated;
return formated;
}

getChatHistory(): BaseChatMessageHistory {
Expand Down
6 changes: 2 additions & 4 deletions src/services/chat-history/redis-chat-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ class RedisChatHistory implements IChatHistory {
return this._history?.getMessages();
}

async getFormatedMessages(): Promise<string> {
const messages = await this._history?.getMessages();

getFormatedMessages(messages: BaseMessage[]): string {
const cut = messages
.slice(-(this._settings?.limit || 5));

const formated = cut.map((message) => `${message._getType().toUpperCase()}: ${message.content}`).join('\n');

return formated;
return formated;
}

getChatHistory(): BaseChatMessageHistory {
Expand Down

0 comments on commit 1487e98

Please sign in to comment.