Created
May 13, 2023 18:08
-
-
Save chwan1/29a6ada70c3e857959f538fe75bcfaa4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: LangChain 2 Template | |
import "@johnlindquist/kit"; | |
import { PromptConfig } from "@johnlindquist/kit"; | |
import { CallbackManager } from "langchain/callbacks"; | |
import { PromptTemplate } from "langchain/prompts"; | |
const { OpenAI } = await import("langchain/llms"); | |
const template = "What is the capital city of {country}?"; | |
const prompt = new PromptTemplate({ | |
inputVariables: ["country"], | |
template: template, // your template without external input | |
}); | |
const countries = [ | |
"United States", | |
"France", | |
"Japan", | |
"Brazil", | |
"Nigeria", | |
"China", | |
"Germany", | |
"India", | |
"Australia", | |
"Canada", | |
]; | |
const result = await form(` | |
<label for="country">Choose a country:</label> | |
<select name="country" id="country"> | |
${countries | |
.map((item) => { | |
return `<option value="${item}">${item}</option>`; | |
}) | |
.join("")} | |
</select> | |
`); | |
console.log(result); | |
const res = await prompt.format({ country: result.country }); | |
// output: "What is the capital city of France?" | |
const llm = new OpenAI({ | |
openAIApiKey: await env("OPENAI_API_KEY", { | |
hint: `Grab a key from <a href="https://platform.openai.com/account/api-keys">here</a>`, | |
}), | |
streaming: true, | |
temperature: 0, | |
callbackManager: CallbackManager.fromHandlers({ | |
handleLLMStart: async () => chat.addMessage(""), | |
handleLLMNewToken: async (token) => chat.pushToken(token), | |
handleLLMError: async (err) => warn(err), | |
}), | |
}); | |
const config: PromptConfig = { | |
ignoreBlur: true, | |
alwaysOnTop: true, | |
input: res, | |
onSubmit: async (input) => { | |
await llm.call(input); | |
}, | |
}; | |
const messages = await chat(config); | |
inspect(messages); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment