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


Guides

My first chatbot

Learn how build your first chatbot in few minutes

Read more

Concepts

Understand the essential concepts for building bots

Read more

Add Functions

The key to learning how to write flows is add-functions.

Read more

Plugins

Unlimitate and start implementing the community plugins.

Read more

Resources

Modularize

Learn how to modularise flows so that you can have a more maintainable bot.

Send Message

How to send a message via HTTP to start conversations, you can send multimedia as well.

Dockerizer

A good practice is to dockerise your bots to make them more maintainable and effective.

Events

Learning about events will make us more fluent when creating chatbots.