Skip to content

Commit

Permalink
add past context into conversation history
Browse files Browse the repository at this point in the history
  • Loading branch information
adhityan committed May 15, 2024
1 parent aa38e32 commit 0c2e9fd
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 151 deletions.
189 changes: 92 additions & 97 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@llm-tools/embedjs",
"version": "0.0.75",
"version": "0.0.76",
"description": "A NodeJS RAG framework to easily work with LLMs and custom datasets",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -51,14 +51,14 @@
],
"homepage": "https://github.com/llm-tools/embedjs#readme",
"dependencies": {
"@huggingface/inference": "^2.6.7",
"@huggingface/inference": "^2.7.0",
"@langchain/anthropic": "^0.1.17",
"@langchain/cohere": "^0.0.8",
"@langchain/community": "^0.0.56",
"@langchain/core": "^0.1.62",
"@langchain/community": "^0.0.57",
"@langchain/core": "^0.1.63",
"@langchain/google-vertexai": "^0.0.14",
"@langchain/mistralai": "^0.0.19",
"@langchain/openai": "^0.0.28",
"@langchain/openai": "^0.0.29",
"axios": "^1.6.8",
"confluence.js": "^1.7.4",
"debug": "^4.3.4",
Expand All @@ -78,20 +78,20 @@
"@types/debug": "^4.1.12",
"@types/html-to-text": "^9.0.4",
"@types/md5": "^2.3.5",
"@types/node": "^20.12.11",
"@types/node": "^20.12.12",
"@types/usetube": "^2.1.2",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"@typescript-eslint/eslint-plugin": "^7.9.0",
"@typescript-eslint/parser": "^7.9.0",
"chromadb": "^1.8.1",
"cohere-ai": "^7.9.5",
"eslint": "^8.56.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"hnswlib-node": "^3.0.0",
"ioredis": "^5.4.1",
"lmdb": "^3.0.8",
"prettier": "^3.2.5",
"rimraf": "^5.0.6",
"rimraf": "^5.0.7",
"typescript": "^5.4.5",
"vectordb": "^0.4.20",
"weaviate-ts-client": "^2.1.1"
Expand Down
4 changes: 2 additions & 2 deletions src/core/rag-application-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export class RAGApplicationBuilder {
this.searchResultCount = 7;
this.initLoaders = true;

this.queryTemplate = `You are a helpful human like chat bot. Use all the provided context to answer the query at the end. Answer in full.
this.queryTemplate = `You are a helpful human like chat bot. Use any provided context and chat history to answer the query at the end. Answer in full.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
Do not use words like context or training data when responding. You can say you may not have all the information but do not say that you are not a reliable source.`;
Do not use words like context or training data when responding. You can say you do not have all the information but do not indicate that you are not a reliable source.`;

this.setModel(SIMPLE_MODELS.OPENAI_GPT3_TURBO);
}
Expand Down
2 changes: 1 addition & 1 deletion src/global/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export type AddLoaderReturn = { entriesAdded: number; uniqueId: string };

export type ConversationHistory = {
message: string;
sender: 'HUMAN' | 'AI';
sender: 'HUMAN' | 'AI' | 'SYSTEM';
};
4 changes: 4 additions & 0 deletions src/interfaces/base-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export abstract class BaseModel {
const result = await this.runQuery(system, userQuery, supportingContext, conversationHistory);

conversationHistory.push({ message: userQuery, sender: 'HUMAN' });
conversationHistory.push({
message: `Old context: ${supportingContext.map((s) => s.pageContent).join('; ')}`,
sender: 'SYSTEM',
});
conversationHistory.push({ message: result, sender: 'AI' });
return result;
}
Expand Down
11 changes: 3 additions & 8 deletions src/models/anthropic-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,9 @@ export class Anthropic extends BaseModel {
pastMessages.push.apply(
pastMessages,
pastConversations.map((c) => {
if (c.sender === 'AI')
return new AIMessage({
content: c.message,
});

return new HumanMessage({
content: c.message,
});
if (c.sender === 'AI') return new AIMessage({ content: c.message });
else if (c.sender === 'SYSTEM') return new SystemMessage({ content: c.message });
else return new HumanMessage({ content: c.message });
}),
);
pastMessages.push(new HumanMessage(`${userQuery}?`));
Expand Down
3 changes: 2 additions & 1 deletion src/models/huggingface-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export class HuggingFace extends BaseModel {
pastMessages,
pastConversations.map((c) => {
if (c.sender === 'AI') return `AI: ${c.message}`;
return `HUMAN: ${c.message}`;
else if (c.sender === 'SYSTEM') return `SYSTEM: ${c.message}`;
else return `HUMAN: ${c.message}`;
}),
);

Expand Down
11 changes: 3 additions & 8 deletions src/models/mistral-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,9 @@ export class Mistral extends BaseModel {
pastMessages.push.apply(
pastMessages,
pastConversations.map((c) => {
if (c.sender === 'AI')
return new AIMessage({
content: c.message,
});

return new HumanMessage({
content: c.message,
});
if (c.sender === 'AI') return new AIMessage({ content: c.message });
else if (c.sender === 'SYSTEM') return new SystemMessage({ content: c.message });
else return new HumanMessage({ content: c.message });
}),
);
pastMessages.push(new HumanMessage(`${userQuery}?`));
Expand Down
11 changes: 3 additions & 8 deletions src/models/ollama-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,9 @@ export class Ollama extends BaseModel {
pastMessages.push.apply(
pastMessages,
pastConversations.map((c) => {
if (c.sender === 'AI')
return new AIMessage({
content: c.message,
});

return new HumanMessage({
content: c.message,
});
if (c.sender === 'AI') return new AIMessage({ content: c.message });
else if (c.sender === 'SYSTEM') return new SystemMessage({ content: c.message });
else return new HumanMessage({ content: c.message });
}),
);
pastMessages.push(new HumanMessage(`${userQuery}?`));
Expand Down
11 changes: 3 additions & 8 deletions src/models/openai-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,9 @@ export class OpenAi extends BaseModel {
pastMessages.push.apply(
pastMessages,
pastConversations.map((c) => {
if (c.sender === 'AI')
return new AIMessage({
content: c.message,
});

return new HumanMessage({
content: c.message,
});
if (c.sender === 'AI') return new AIMessage({ content: c.message });
else if (c.sender === 'SYSTEM') return new SystemMessage({ content: c.message });
else return new HumanMessage({ content: c.message });
}),
);
pastMessages.push(new HumanMessage(`${userQuery}?`));
Expand Down
11 changes: 3 additions & 8 deletions src/models/vertexai-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,9 @@ export class VertexAI extends BaseModel {
pastMessages.push.apply(
pastMessages,
pastConversations.map((c) => {
if (c.sender === 'AI')
return new AIMessage({
content: c.message,
});

return new HumanMessage({
content: c.message,
});
if (c.sender === 'AI') return new AIMessage({ content: c.message });
else if (c.sender === 'SYSTEM') return new SystemMessage({ content: c.message });
else return new HumanMessage({ content: c.message });
}),
);
pastMessages.push(new HumanMessage(`${userQuery}?`));
Expand Down

0 comments on commit 0c2e9fd

Please sign in to comment.