# 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?
>
> - **Canonical URL:** https://www.builderbot.app/en/tutorials/langchain
> - **Documentation Index:** https://www.builderbot.app/llms.txt
> - **Full Corpus:** https://www.builderbot.app/llms-full.txt

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

```bash
pnpm i @langchain/openai @langchain/core zod
```

```ts (ai/catch-intention.ts)
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');
    }
};
```
```ts (app.ts)
import { createBot, MemoryDB, createProvider, createFlow } from '@builderbot/bot'
import { MetaProvider } from '@builderbot/provider-meta'
import { getIntention } from './ai/catch-intention'

const PORT = process.env.PORT ?? 3001

const welcome = addKeyword(EVENTS.WELCOME)
    .addAction(async (ctx, { gotoFlow }) => {
        const intention = getIntention(ctx.body)

        if (intention === 'greeting') {
            return gotoFlow(anyFlowThatYouDecide)
        } else if (intention === 'sales') {
            /* sales flow */
            /* any flow */
        }
    })

const main = async () => {
    const provider = createProvider(MetaProvider)

    const { handleCtx, httpServer } = await createBot({
        database: new MemoryDB(),
        provider,
        flow: createFlow([welcome]),
    })

    httpServer(+PORT)

    provider.server.post('/v1/register', handleCtx(async (bot, req, res) => {
        const { number, name } = req.body
        await bot.dispatch('EVENT_REGISTER', { from: number, name })
        return res.end('trigger')
    }))
}

main()
```

That way you can validate the intentions of your end customer and set up your own purchase flow as easy as that

---
