Langchain
When we want something more personality and intelligence in our assistants, the first thing we think about is Openai, what if I tell you that there is a simple way to get the most out of your LLM?
Install
let's look at a simple but very valuable trick. to be able to know the user's intention, we have tried it before with DialogFlow but what a headache, let's go for something easier
pnpm i @langchain/openai @langchain/core zod
import { z } from "zod";
import { ChatOpenAI, ChatPromptTemplate } from "@langchain/openai";
export const openAI = new ChatOpenAI({
modelName: 'gpt-4',
openAIApiKey: 'YOUR_API_KEY_HERE',
});
const SYSTEM_STRUCT = `just only history based:
{history}
Answer the users question as best as possible.`;
export const PROMPT_STRUCT = ChatPromptTemplate.fromMessages([
["system", SYSTEM_STRUCT],
["human", "{question}"]
]);
const catchIntention = z.object(
{
intention: z.enum(['UNKNOWN', 'SALES', 'GREETING', 'CLOSURE'])
.describe('Categorize the following conversation and decide what the intention is')
}
).describe('Given the following products, you should structure it in the best way, do not alter or edit anything');
const llmWithToolsCatchIntention = openAI.withStructuredOutput(catchIntention, {
name: "CatchIntention",
});
export const getIntention = async (text: string): Promise<string> => {
try {
const { intention } = await PROMPT_STRUCT.pipe(llmWithToolsCatchIntention).invoke({
question: text,
history: await history.getHistory(state)
});
return Promise.resolve(String(intention).toLocaleLowerCase());
} catch (errorIntention) {
return Promise.resolve('unknown');
}
};
That way you can validate the intentions of your end customer and set up your own purchase flow as easy as that