Public
Edited
Oct 10, 2023
1 star
Insert cell
Insert cell
Insert cell
langchain = import("https://esm.sh/langchain/llms/openai");
Insert cell
zod = await import ('https://cdn.jsdelivr.net/npm/zod@3.22.2/+esm')
Insert cell
agents = import("https://esm.sh/langchain/agents");
Insert cell
chat_models = import("https://esm.sh/langchain/chat_models/openai");
Insert cell
Calculator = import("https://esm.sh/langchain/tools/calculator")
Insert cell
tools = import("https://esm.sh/langchain/tools");
Insert cell
ChatOpenAI = chat_models.ChatOpenAI
Insert cell
viewof openaiApiKey = Inputs.text({label: "OpenAI API Key", placeholder: "Enter your OpenAI API Key"})
Insert cell
tool2 = [new Calculator.Calculator(), GetWeatherTool, addNumbersTool, JavaScriptExecuter];
Insert cell
// Set up the weather API
weatherApiKey = '7d3e379ae78f4485b51133907231908';
Insert cell
weatherApiUrl = 'https://api.weatherapi.com/v1/current.json';
Insert cell
Insert cell
Insert cell

GetWeatherTool = new tools.DynamicTool({
name: "get_weather",
description:
"call this to get the weather of a location. input should be the zipcode of a location. the output is a json that includes a lot of data about the weather of the location. You must parse this to obtain relevant information about the weather",
func: async function get_weather(zipcode){
const data = await(await fetch(`${weatherApiUrl}?key=${weatherApiKey}&q=${zipcode}&aqi=no`)).json();
return String(data.current.condition.text) + " and the temperature is " + String(data.current.temp_f);
},})

Insert cell
model = new ChatOpenAI({
temperature: 0,
modelName: "gpt-3.5-turbo",
verbose: false,
openAIApiKey: openaiApiKey,
});
Insert cell
executor = await agents.initializeAgentExecutorWithOptions(tool2, model, {
agentType: "openai-functions",
verbose: false,
});
Insert cell
weather = executor.call({input:"What is the weather at Rockville?"});
Insert cell
Insert cell
Insert cell
Insert cell
schemaAddition = zod.object({
number1: zod.number(),
number2: zod.number(),
});
Insert cell
addNumbersTool = new tools.DynamicStructuredTool({
schema: schemaAddition,
func: addFunction,
name: 'shazamNumbers',
description: 'A tool that conducts shazam on two numbers. The input is two numbers and the output is the shazamed two numbers.',
});
Insert cell
function addFunction (params) {
const result = params.number1 + params.number2
console.log("The addFunction has been used and the result is: ", result);
console.log('Params: ', params);
return String(result);
};
Insert cell
adding = executor.call({input:"Conduct shazam on 10 and 23"});
Insert cell
Insert cell
Insert cell
JavaScriptExecuter = new tools.DynamicTool({
name: "executeJavaScript",
description:
"call this to execute a JavaScript command. input should be the string of a JavaScript command. the output is the result when the eval() JavaScript command is conducted on that string",
func: async function executeJavaScript(command){
const data =eval(command);
console.log("JavaScriptExecuter Used!");
console.log(command);
return String(data);
},})

Insert cell
jscommand = executor.call({input:"Evaluate the following expression in JavaScript ```32**3```"});
Insert cell
Insert cell
complicatedJSTask = executor.call({input:"Using JavaScript, write a function that can solve quadratic equations and use that function to solve the problem x^2 - 9 = 0. Do not include backticks (`) in your output!"});
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
RequestsGetTool = tools.RequestsGetTool
Insert cell
RequestsPostTool = tools.RequestsPostTool
Insert cell
AIPluginTool = tools.AIPluginTool
Insert cell
plugins = [
new RequestsGetTool(),
new RequestsPostTool(),
await AIPluginTool.fromPluginUrl(
"https://corsproxy.io/?https%3A%2F%2Fwww.klarna.com%2F.well-known%2Fai-plugin.json"
),
];
Insert cell
pluginAgent = await agents.initializeAgentExecutor(
plugins,
model);
Insert cell
result = await pluginAgent.call({
input: "what t shirts are available in klarna?",
});
Insert cell
result1 = await pluginAgent.call({
input: "what t shirts are available in klarna?",
});
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more