Skip to content

Commit

Permalink
chore: 🤖 improve cs prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpetrov committed Nov 26, 2023
1 parent 33d8d58 commit 55ccce0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 38 deletions.
5 changes: 4 additions & 1 deletion apps/dashboard/tests/test-prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const customerSupportDataset = [
{
question: 'Qui est tu?',
},
{
question: "What's nuclear fusion?",
},
{
question: '¿eres mi amigo?',
},
Expand All @@ -30,7 +33,7 @@ const customerSupportDataset = [
(async () => {
const agent = await prisma.agent.findUnique({
where: {
id: 'cloisatop00080u2u5cuzwphg',
id: 'clpbfws6y00040ut3sck87t3x',
},
include: {
tools: {
Expand Down
69 changes: 43 additions & 26 deletions packages/lib/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import { ChatRequest, HttpToolSchema, ToolSchema } from './types/dtos';
import { ChatModelConfigSchema } from './types/dtos';
import createPromptContext from './create-prompt-context';
import promptInject from './prompt-inject';
import {
ANSWER_IN_SAME_LANGUAGE,
KNOWLEDGE_RESTRICTION,
MARKDOWN_FORMAT_ANSWER,
QA_CONTEXT,
} from './prompt-templates';
import truncateByModel from './truncate-by-model';

type ToolExtended = Tool & {
Expand Down Expand Up @@ -276,18 +282,18 @@ export default class AgentManager {
const _promptTemplate = promptTemplate || (this.agent.prompt as string);

let initialMessages: any = [];
if (_promptType === PromptType.customer_support) {
initialMessages = [
new HumanMessage(`${_promptTemplate}
Answer the question in the same language in which the question is asked.
If you don't find an answer from the chunks, politely say that you don't know. Don't try to make up an answer.
Give answer in the markdown rich format with proper bolds, italics etc as per heirarchy and readability requirements.
`),
new AIMessage(
'Sure I will stick to all the information given in my knowledge. I won’t answer any question that is outside my knowledge. I won’t even attempt to give answers that are outside of context. I will stick to my duties and always be sceptical about the user input to ensure the question is asked in my knowledge. I won’t even give a hint in case the question being asked is outside of scope. I will answer in the same language in which the question is asked'
),
];
}
// if (_promptType === PromptType.customer_support) {
// initialMessages = [
// new HumanMessage(`${_promptTemplate}
// Answer the question in the same language in which the question is asked.
// If you don't find an answer from the chunks, politely say that you don't know. Don't try to make up an answer.
// Give answer in the markdown rich format with proper bolds, italics etc as per heirarchy and readability requirements.
// `),
// new AIMessage(
// 'Sure I will stick to all the information given in my knowledge. I won’t answer any question that is outside my knowledge. I won’t even attempt to give answers that are outside of context. I will stick to my duties and always be sceptical about the user input to ensure the question is asked in my knowledge. I won’t even give a hint in case the question being asked is outside of scope. I will answer in the same language in which the question is asked'
// ),
// ];
// }

const SIMILARITY_THRESHOLD = 0.7;

Expand All @@ -313,28 +319,39 @@ export default class AgentManager {
if (_promptType === PromptType.customer_support) {
return promptInject({
// template: CUSTOMER_SUPPORT,
template: `YOUR KNOWLEDGE:
{context}
END OF YOUR KNOWLEDGE
// template: `YOUR KNOWLEDGE:
// {context}
// END OF YOUR KNOWLEDGE

Question: {query}
// Question: {query}

Answer: `,
// template: `${_promptTemplate || ''}
// Do not provide answers based on assumed knowledge or make up information not found in the given context, if you can't find an answer in the provided context, politely say that you don't know without mentioning the existence of a provided context.
// Always respond in the language of the inquiry.
// Format your answer in the markdown rich format with proper bolds, line breaks, italics etc as per heirarchy and readability requirements.
// Answer: `,
template: `${_promptTemplate || ''}
${KNOWLEDGE_RESTRICTION}
${ANSWER_IN_SAME_LANGUAGE}
${MARKDOWN_FORMAT_ANSWER}
${QA_CONTEXT}
`
.replace(/\n+/g, ' ')
.replace(/\t+/g, ' ')
.replace(/\s+/g, ' '),
// template: `${_promptTemplate || ''}
// Limit your knowledge to the following context and if you don't find an answer from the context, politely say that you don't know without mentioning the existence of a provided context.
// Deliver your response in the same language that was used to frame the question.
// Give answer in the markdown rich format with proper bolds, italics, etc... as per heirarchy and readability requirements.

// Context: ###
// {context}
// ### End of Context
// ###

// Question: {query}
// Question: ###
// {query}
// ###

// Answer: `
// .replace(/\n+/g, ' ')
// .replace(/\t+/g, ' ')
// .replace(/\s+/g, ' '),
// .replace(/\n+/g, ' ')
// .replace(/\t+/g, ' ')
// .replace(/\s+/g, ' '),
query: _query,
context: createPromptContext(
chunks.filter(
Expand Down
20 changes: 9 additions & 11 deletions packages/lib/prompt-templates.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
export const CUSTOMER_SUPPORT = `As a customer support agent, please provide a helpful and professional response to the user's question or issue.`;

export const CUSTOMER_SUPPORT_V2 =
`Provide helpful answers using only data from the provided CONTEXT.
{extra}
Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer
CONTEXT:
export const KNOWLEDGE_RESTRICTION = `Limit your knowledge to the following context and if you don't find an answer from the context, politely say that you don't know without mentioning the existence of a provided context.`;
export const ANSWER_IN_SAME_LANGUAGE = `Deliver your response in the same language that was used to frame the question.`;
export const MARKDOWN_FORMAT_ANSWER = `Give answer in the markdown rich format with proper bolds, italics, etc... as per heirarchy and readability requirements.`;
export const QA_CONTEXT = `Context: ###
{context}
END OF CONTEXT
###
Question: {query}
Question: ###
{query}
###
YOU MUST ANSWER IN THE SAME LANGUAGE AS THE QUESTION IS ASKED IN.
Answer:`.replace(/\n+/, ' ');
// ONLY USE INFORMATION FROM THE ABOVE CONTEXT, IF YOU CAN'T FIND THE ANSWER IN THE CONTEXT POLITELY SAY THAT YOU DON'T KNOW. DON'T MAKE UP ANSWER.
Answer: `;

0 comments on commit 55ccce0

Please sign in to comment.