Erstellen Sie einen leistungsstarken Chatbot mit OpenAI und LangChain
Introduction
Chatbots are essential tools in various industries, providing automated interaction with users. There is no person in the world these days that have not tried at least once Chat GPT (or any other AI powered chatbot). Using OpenAI’s GPT models and the LangChain library, we can build a chatbot that handles sessions and processes user messages through a streaming response system, as in later post we will comunicate with our API's and make agents which will be specialized for certain things.
Here’s what we’ll cover:
- Setting up an Express server with middleware.
- Creating an `AgentManager` to handle chatbot agents.
- Creating an ChatAgent to handle chatbot agents.
- Streaming chatbot responses back to users in real-time.
Setting Up the Environment
First, we need a few key dependencies:
- Express for handling API requests.
- LangChain to manage GPT models and tools.
- OpenAI for GPT model interaction. We need to obtain token from Open AI in order to use spawn sessions and interact with chatbot
Install Dependencies
First thing we do is to initialize new project and install neccessarry modules which we will use.
npm init -Y npm install express langchain openai uuid class-validator class-transformer mutex
Setting Up Express Routes
To begin, we'll define two main routes:
First route will create a new chat session, whereas second one will send messages to an existing session.
router.post('/session', APIKeyMiddleware, createSession); router.post('/session/:id/message', APIKeyMiddleware, postMessage);
The APIKeyMiddleware ensures that only authenticated requests access these routes. Note that you can implement middleware which suits yours needs.
Creating Agent Manager
We’ll create an AgentManager class to handle chat agents. This class is responsible for creating new agents and managing active sessions, so imagine this class as the main entrypoint for our API's as it will hadle agents which will be responsible for chat. First user will need to create session and later on that session will be used for chatting.
export class AgentManager { private __lock = new Mutex(); private __agents: Map<string, AgentInstance> = new Map(); async createAgent(authorization: string): Promise<string> { const uuid = uuidv4(); const release = await this.__lock.acquire(); try { this.__deleteExpiredAgentsLockless(); let agent: ChatAgent | null = agent = new GeneralChatAgent(authorization); this.__agents.set(uuid, { agent, createdAt: Date.now() }); return uuid; } finally { release(); } } async getAgent(uuid: string): Promise<ChatAgent | null> { const release = await this.__lock.acquire(); try { this.__deleteExpiredAgentsLockless(); const agentInstance = this.__agents.get(uuid); return agentInstance ? agentInstance.agent : null; } finally { release(); } } private __deleteExpiredAgentsLockless(): void {} }
Creating General Agent
Now we need to create general chat agent, which will get parameters with lets say for example auth or any other you need and can be able to communicate with API, but for now we will extend existing ChatAgent and nothing more for this step.
export class GeneralChatAgent extends ChatAgent { constructor() { super(); } }
The createAgent method initializes an agent, locks the process, and assigns it to a unique session ID. Agents expire after a specified session duration, which is handled by the __deleteExpiredAgentsLockless method but we will implement it in the next itteration you can avoid it for now.
Handling Sessions and Messages
Next, let's define our session creation and message handling routes:
export const createSession = async (req: Request, res: Response): Promise<void> => { const authorization = req.headers['authorization'] as string; try { const sessionId = await agentManager.createAgent(authorization, AgentType.WEB); res.json({ sessionId }); } catch (err) { if (err instanceof Error) { res.status(400).json({ error: err.message }); } else { res.status(500).json({ error: 'An unknown error occurred' }); } } } export const postMessage = async (req: Request, res: Response): Promise<void> => { const { id } = req.params; const { message } = req.body; if (!id || !message) { return res.status(400).json({ error: 'Bad request. Missing session ID or message' }); } try { const agent = await agentManager.getAgent(id); if (!agent) { return res.status(400).json({ error: `No agent found with id ${id}` }); } const iterable = await agent.invoke(message); await streamResponse(res, iterable); } catch (err) { res.status(500).json({ error: err instanceof Error ? err.message : 'An unknown error occurred' }); } }
Here, createSession sets up a new session and postMessage sends a user’s message to the agent. If no session or message is provided, it returns a 400 Bad Request error.
Streaming Responses
Now, the key to making our chat bot feel responsive and interactive: streaming the response.
async invoke(input: string): Promise<AsyncIterable<Chunk>> { const release = await this.__lock.acquire(); try { const tool = this.determineTool(input); if (tool) { const toolOutput = await tool.call(input); this.callbackQueue.enqueue({ type: ChunkType.TOKEN, value: toolOutput }); this.callbackQueue.enqueue({ type: ChunkType.FINISH, value: '' }); } else { await this.chat.invoke([new HumanMessage(input)], { callbacks: [ { handleLLMNewToken: (token: string) => { this.callbackQueue.enqueue({ type: ChunkType.TOKEN, value: token }); }, handleLLMEnd: () => { this.callbackQueue.enqueue({ type: ChunkType.FINISH, value: '' }); }, handleLLMError: (error: Error) => { this.callbackQueue.enqueue({ type: ChunkType.ERROR, value: error.message }); } } ] }); } return this.createAsyncIterable(this.callbackQueue); } finally { release(); } } private createAsyncIterable(callbackQueue: AgentCallbackQueue): AsyncIterable<Chunk> { return { [Symbol.asyncIterator]: async function* () { let finished = false; while (!finished) { const chunk = await callbackQueue.dequeue(); if (chunk) { yield chunk; if (chunk.type === ChunkType.FINISH || chunk.type === ChunkType.ERROR) { finished = true; } } else { await new Promise(resolve => setTimeout(resolve, 100)); } } } }; }
In the invoke method, the agent processes the user’s input and streams back the response in chunks. Each chunk is either a token from the model or a message indicating the end of the stream.
The createAsyncIterable method allows us to generate these chunks one by one and stream them back to the client.
Streaming response
In the end, we want to stream response to client as we recieve it, dont want to wait for some time until completes and return the whole response, better solution is to stream response in chunks.
const delay = (ms: number): Promise<void> => new Promise(resolve => setTimeout(resolve, ms)); export async function streamResponse(res: Response, iterable: AsyncIterable<Chunk>) { res.setHeader('Content-Type', 'application/x-ndjson'); res.setHeader('Transfer-Encoding', 'chunked'); try { let buffer = ''; for await (const chunk of iterable) { switch (chunk.type) { case ChunkType.TOKEN: buffer += chunk.value; res.write(buffer); if (res.flush) res.flush(); buffer = ''; break; case ChunkType.ERROR: console.error('Error chunk:', chunk.value); if (!res.headersSent) { res.status(500).json({ error: 'Streaming failed.' }); } return; case ChunkType.FINISH: if (buffer.trim()) { res.write(`${buffer.trim()}\n`); } return; } } } catch (err) { console.error('Error during streaming:', err); if (!res.headersSent) { res.status(500).json({ error: 'Streaming failed.' }); } } finally { res.end(); } }
Conclusion
Congratulations! You now have a basic chatbot that handles chat sessions and streams responses back to the client. This architecture can be easily extended with additional tools, more sophisticated logic, or different GPT models, but for now we have skeleton for more complex chatbot.
By using OpenAI’s powerful language models and LangChain’s tool management, you can create more advanced and interactive chatbots for various domains.You can expand the chatbots capabilities and make it in a way that you want but on the other hand you dont need to use Langchain , you can use OpenAI and make even more simpler chat bot if you prefer that way.
Stay tuned for more , in next post we will talk about building tools for chat agent we made
Happy coding!
Feel free to check original post
Das obige ist der detaillierte Inhalt vonErstellen Sie einen leistungsstarken Chatbot mit OpenAI und LangChain. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

Video Face Swap
Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen











Unterschiedliche JavaScript -Motoren haben unterschiedliche Auswirkungen beim Analysieren und Ausführen von JavaScript -Code, da sich die Implementierungsprinzipien und Optimierungsstrategien jeder Engine unterscheiden. 1. Lexikalanalyse: Quellcode in die lexikalische Einheit umwandeln. 2. Grammatikanalyse: Erzeugen Sie einen abstrakten Syntaxbaum. 3. Optimierung und Kompilierung: Generieren Sie den Maschinencode über den JIT -Compiler. 4. Führen Sie aus: Führen Sie den Maschinencode aus. V8 Engine optimiert durch sofortige Kompilierung und versteckte Klasse.

Python eignet sich besser für Anfänger mit einer reibungslosen Lernkurve und einer kurzen Syntax. JavaScript ist für die Front-End-Entwicklung mit einer steilen Lernkurve und einer flexiblen Syntax geeignet. 1. Python-Syntax ist intuitiv und für die Entwicklung von Datenwissenschaften und Back-End-Entwicklung geeignet. 2. JavaScript ist flexibel und in Front-End- und serverseitiger Programmierung weit verbreitet.

Die Verschiebung von C/C zu JavaScript erfordert die Anpassung an dynamische Typisierung, Müllsammlung und asynchrone Programmierung. 1) C/C ist eine statisch typisierte Sprache, die eine manuelle Speicherverwaltung erfordert, während JavaScript dynamisch eingegeben und die Müllsammlung automatisch verarbeitet wird. 2) C/C muss in den Maschinencode kompiliert werden, während JavaScript eine interpretierte Sprache ist. 3) JavaScript führt Konzepte wie Verschlüsse, Prototypketten und Versprechen ein, die die Flexibilität und asynchrone Programmierfunktionen verbessern.

Zu den Hauptanwendungen von JavaScript in der Webentwicklung gehören die Interaktion der Clients, die Formüberprüfung und die asynchrone Kommunikation. 1) Dynamisches Inhaltsaktualisierung und Benutzerinteraktion durch DOM -Operationen; 2) Die Kundenüberprüfung erfolgt vor dem Einreichung von Daten, um die Benutzererfahrung zu verbessern. 3) Die Aktualisierung der Kommunikation mit dem Server wird durch AJAX -Technologie erreicht.

Die Anwendung von JavaScript in der realen Welt umfasst Front-End- und Back-End-Entwicklung. 1) Zeigen Sie Front-End-Anwendungen an, indem Sie eine TODO-Listanwendung erstellen, die DOM-Operationen und Ereignisverarbeitung umfasst. 2) Erstellen Sie RESTFUFFUPI über Node.js und express, um Back-End-Anwendungen zu demonstrieren.

Es ist für Entwickler wichtig, zu verstehen, wie die JavaScript -Engine intern funktioniert, da sie effizientere Code schreibt und Leistungs Engpässe und Optimierungsstrategien verstehen kann. 1) Der Workflow der Engine umfasst drei Phasen: Parsen, Kompilieren und Ausführung; 2) Während des Ausführungsprozesses führt die Engine dynamische Optimierung durch, wie z. B. Inline -Cache und versteckte Klassen. 3) Zu Best Practices gehören die Vermeidung globaler Variablen, die Optimierung von Schleifen, die Verwendung von const und lass und die Vermeidung übermäßiger Verwendung von Schließungen.

Python und JavaScript haben ihre eigenen Vor- und Nachteile in Bezug auf Gemeinschaft, Bibliotheken und Ressourcen. 1) Die Python-Community ist freundlich und für Anfänger geeignet, aber die Front-End-Entwicklungsressourcen sind nicht so reich wie JavaScript. 2) Python ist leistungsstark in Bibliotheken für Datenwissenschaft und maschinelles Lernen, während JavaScript in Bibliotheken und Front-End-Entwicklungsbibliotheken und Frameworks besser ist. 3) Beide haben reichhaltige Lernressourcen, aber Python eignet sich zum Beginn der offiziellen Dokumente, während JavaScript mit Mdnwebdocs besser ist. Die Wahl sollte auf Projektbedürfnissen und persönlichen Interessen beruhen.

Sowohl Python als auch JavaScripts Entscheidungen in Entwicklungsumgebungen sind wichtig. 1) Die Entwicklungsumgebung von Python umfasst Pycharm, Jupyternotebook und Anaconda, die für Datenwissenschaft und schnelles Prototyping geeignet sind. 2) Die Entwicklungsumgebung von JavaScript umfasst Node.JS, VSCODE und WebPack, die für die Entwicklung von Front-End- und Back-End-Entwicklung geeignet sind. Durch die Auswahl der richtigen Tools nach den Projektbedürfnissen kann die Entwicklung der Entwicklung und die Erfolgsquote der Projekte verbessert werden.
