|
| 1 | +--- |
| 2 | +id: "index" |
| 3 | +title: "Quickstart" |
| 4 | +sidebar_label: "Quickstart" |
| 5 | +sidebar_position: 0.1 |
| 6 | +--- |
| 7 | + |
| 8 | +# Quickstart Guide |
| 9 | + |
| 10 | +This tutorial gives you a walkthrough on how to quickly connect AI agents to the ConvoStack chatbot playground. |
| 11 | + |
| 12 | +We will be using **Langchain** for creating the AI agents and **ConvoStack** for connecting these agents to a production-ready chatbot playground. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```bash |
| 19 | +npm install convostack langchain |
| 20 | +``` |
| 21 | + |
| 22 | +## Example 1: OpenAI Agent |
| 23 | + |
| 24 | +In this example, we are connecting an OpenAI [LLM](https://js.langchain.com/docs/modules/models/llms/) to the chatbot playground. |
| 25 | + |
| 26 | +```typescript |
| 27 | +import { playground } from "convostack/playground"; |
| 28 | +import { OpenAI } from "langchain/llms/openai"; |
| 29 | + |
| 30 | +playground({ |
| 31 | + reply(context: IAgentContext): Promise<IAgentResponse> { |
| 32 | + // `humanMessage` is the content of each message the user sends via the chatbot playground. |
| 33 | + let humanMessage = context.getHumanMessage().content; |
| 34 | + // `agent` is the OpenAI agent we want to use to respond to each `humanMessage` |
| 35 | + const agent = new OpenAI(); |
| 36 | + // `call` is a simple string-in, string-out method for interacting with the OpenAI agent. |
| 37 | + const resp = await model.call(humanMessage); |
| 38 | + // `resp` is the generated agent's response to the user's `humanMessage` |
| 39 | + return { |
| 40 | + content: resp, |
| 41 | + contentType: "markdown", |
| 42 | + }; |
| 43 | + }, |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +**See the code above in action:** |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +## Example 2: LLM Chain |
| 52 | + |
| 53 | +In this example, we are constructing an [LLMChain](https://js.langchain.com/docs/modules/chains/llm_chain) which takes a human message from the chatbot playground, formats it with a [PromptTemplate](https://js.langchain.com/docs/modules/prompts/prompt_templates/), and then passes the formatted response to an OpenAI agent. |
| 54 | + |
| 55 | +The generated response of the agent will be streamed to the user via the chatbot playground. |
| 56 | + |
| 57 | +```typescript |
| 58 | +import { playground } from "convostack/playground"; |
| 59 | +import { |
| 60 | + ChatPromptTemplate, |
| 61 | + HumanMessagePromptTemplate, |
| 62 | + SystemMessagePromptTemplate, |
| 63 | +} from "langchain/prompts"; |
| 64 | +import { LLMChain } from "langchain/chains"; |
| 65 | +import { ChatOpenAI } from "langchain/chat_models/openai"; |
| 66 | + |
| 67 | +playground({ |
| 68 | + reply(context: IAgentContext): Promise<IAgentResponse> { |
| 69 | + // `humanMessage` is the content of each message the user sends via the chatbot playground. |
| 70 | + let humanMessage = context.getHumanMessage().content; |
| 71 | + // We can now construct an LLMChain from a ChatPromptTemplate and a chat model. |
| 72 | + const chat = new ChatOpenAI({ streaming: true, temperature: 0 }); |
| 73 | + // Pre-prompt the agent to be a language translator |
| 74 | + const chatPrompt = ChatPromptTemplate.fromPromptMessages([ |
| 75 | + SystemMessagePromptTemplate.fromTemplate( |
| 76 | + "You are a helpful assistant that translates {input_language} to {output_language}." |
| 77 | + ), |
| 78 | + HumanMessagePromptTemplate.fromTemplate("{text}"), |
| 79 | + ]); |
| 80 | + const chain = new LLMChain({ |
| 81 | + prompt: chatPrompt, |
| 82 | + llm: chat, |
| 83 | + }); |
| 84 | + |
| 85 | + // `resp` is the response of the OpenAI LLM chain translating `humanMessage` from English to French. |
| 86 | + const resp = await chain.call({ |
| 87 | + input_language: "English", |
| 88 | + output_language: "French", |
| 89 | + text: humanMessage, |
| 90 | + }); |
| 91 | + |
| 92 | + return { |
| 93 | + content: resp.text, |
| 94 | + contentType: "markdown", |
| 95 | + }; |
| 96 | + }, |
| 97 | +}); |
| 98 | +``` |
| 99 | + |
| 100 | +**See the code above in action:** |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +## Example 3: LLM Chain With History |
| 105 | + |
| 106 | +In this example, we are connecting an OpenAI [LLM](https://js.langchain.com/docs/modules/models/llms/) that remembers the previous conversational back and forths directly using [Buffer Memory](https://js.langchain.com/docs/modules/memory/examples/buffer_memory) and `ConvoStackLangchainChatMessageHistory`. |
| 107 | + |
| 108 | +The generated response of the agent will be streamed to the user via the chatbot playground. |
| 109 | + |
| 110 | +```typescript |
| 111 | +import { playground } from "convostack/playground"; |
| 112 | +import { ConvoStackLangchainChatMessageHistory } from "convostack/langchain-memory"; |
| 113 | +import { ChatOpenAI } from "langchain/chat_models/openai"; |
| 114 | +import { |
| 115 | + SystemMessagePromptTemplate, |
| 116 | + HumanMessagePromptTemplate, |
| 117 | + ChatPromptTemplate, |
| 118 | + MessagesPlaceholder, |
| 119 | +} from "langchain/prompts"; |
| 120 | +import { ConversationChain } from "langchain/chains"; |
| 121 | +import { BufferMemory } from "langchain/memory"; |
| 122 | + |
| 123 | +playground({ |
| 124 | + reply( |
| 125 | + context: IAgentContext, |
| 126 | + callbacks?: IAgentCallbacks |
| 127 | + ): Promise<IAgentResponse> { |
| 128 | + // `humanMessage` is the content of each message the user sends via the chatbot playground. |
| 129 | + let humanMessage = context.getHumanMessage().content; |
| 130 | + |
| 131 | + // Create a new OpenAI agent, with streaming |
| 132 | + const chat = new ChatOpenAI({ |
| 133 | + modelName: "gpt-3.5-turbo", |
| 134 | + temperature: 0, |
| 135 | + streaming: true, |
| 136 | + callbacks: [ |
| 137 | + { |
| 138 | + handleLLMNewToken(token: string) { |
| 139 | + // Stream tokens to ConvoStack |
| 140 | + callbacks.onMessagePart({ |
| 141 | + contentChunk: token, |
| 142 | + }); |
| 143 | + }, |
| 144 | + }, |
| 145 | + ], |
| 146 | + }); |
| 147 | + |
| 148 | + // Setup your prompts (note the placeholder for {history}) |
| 149 | + const chatPrompt = ChatPromptTemplate.fromPromptMessages([ |
| 150 | + SystemMessagePromptTemplate.fromTemplate( |
| 151 | + "The following is a friendly conversation between a human and an AI." |
| 152 | + ), |
| 153 | + new MessagesPlaceholder("history"), |
| 154 | + HumanMessagePromptTemplate.fromTemplate("{input}"), |
| 155 | + ]); |
| 156 | + |
| 157 | + // Setup the chain with a BufferMemory that pulls from the ConvoStack conversation history |
| 158 | + const chain = new ConversationChain({ |
| 159 | + memory: new BufferMemory({ |
| 160 | + // Use the ConvoStackLangchainChatMessageHistory class to prepare a Langchain-compatible version of the history |
| 161 | + chatHistory: new ConvoStackLangchainChatMessageHistory({ |
| 162 | + // Pass the current conversation's message history for loading |
| 163 | + history: context.getHistory(), |
| 164 | + }), |
| 165 | + returnMessages: true, |
| 166 | + memoryKey: "history", |
| 167 | + }), |
| 168 | + prompt: chatPrompt, |
| 169 | + llm: chat, |
| 170 | + }); |
| 171 | + |
| 172 | + // `resp` is the response of the OpenAI LLM chain to `humanMessage`, which was inputted on the ConvoStack playground. |
| 173 | + const resp = await chain.call({ |
| 174 | + input: context.getHumanMessage().content, |
| 175 | + }); |
| 176 | + |
| 177 | + // Send the final response to ConvoStack |
| 178 | + return { |
| 179 | + content: resp.response, |
| 180 | + contentType: "markdown", |
| 181 | + }; |
| 182 | + }, |
| 183 | +}); |
| 184 | +``` |
| 185 | + |
| 186 | +**See the code above in action:** |
| 187 | + |
| 188 | + |
0 commit comments