Search
Code28
export default async function(req: Request): Promise<Response> { // Read secrets from x-secrets header const apiKey = __secrets['openai_api_key']; if (!apiKey) { console.error('OpenAI API key not found in environment'); return Response.json( { error: 'OpenAI API key not configured' }, { status: 500 } console.log('Calling OpenAI API with prompt:', prompt); // Call OpenAI API const response = await fetch('https://api.openai.com/v1/chat/completions', { method: 'POST', const error = await response.text(); console.error('OpenAI API error:', response.status, error); return Response.json( { error: `OpenAI API error: ${response.status} - ${error}` }, { status: 500 } const data = await response.json(); console.log('OpenAI response received'); import { createToolCallingAgent } from 'npm:langchain/agents'import { AgentExecutor } from 'npm:langchain/agents'import { ChatPromptTemplate } from 'npm:@langchain/core/prompts'import { OpenAI } from "https://esm.town/v/std/openai";import { WikipediaQueryRun } from 'npm:@langchain/community/tools/wikipedia_query_run'export async function agentExample() { const llm = new OpenAI() const agent = createToolCallingAgent({ llm: llm, tools: tools, prompt }) const agentExecutor = new AgentExecutor({ agent, tools })# askSMHIUsing OpenAI chat completion with function calls to [SMHI](https://en.wikipedia.org/wiki/Swedish_Meteorological_and_Hydrological_Institute) api* [SMHI, forecast documentation](https://opendata.smhi.se/apidocs/metfcst/get-forecast.html)* [OPEN AI, GPT function calling documentation](https://platform.openai.com/docs/guides/function-calling?api-mode=chat&lang=javascript)2. Send the question to Open AI API moderation3. Create tool calling by converting schema to JSON schema4. Send the question to Open AI Chat Completion and expose tool calling5. Make the API call to the SMHI API with parameters from OPEN AI## Enviroment variables* OPENAI_CHAT: Needs to be authorized to write chat completions and to the moderation API.## Packages used* openai: For typesafe API request and responses* valibot: for describing the SMHI API response and function API input* valibot/to-json-schema: Transform the schema to json schema (readable by the GPT API)You can chat with llms over email, the email thread functions as memory. The biggest thing is that you can instantly create a chatlike interface with llms. Pair that with back end data and functions and you got something really powerful.### Toolings* Llms can uses [tools](https://platform.openai.com/docs/guides/function-calling), meaning you can make this an agent and a whole lot more useful. You can chat with llms over email, the email thread functions as memory. The biggest thing is that you can instantly create a chatlike interface with llms. Pair that with back end data and functions and you got something really powerful.### Toolings* Llms can uses [tools](https://platform.openai.com/docs/guides/function-calling), meaning you can make this an agent and a whole lot more useful. You can chat with llms over email, the email thread functions as memory. The biggest thing is that you can instantly create a chatlike interface with llms. Pair that with back end data and functions and you got something really powerful.### Toolings* Llms can uses [tools](https://platform.openai.com/docs/guides/function-calling), meaning you can make this an agent and a whole lot more useful. import { OpenAI } from "https://esm.town/v/std/openai?v=4";const openai = new OpenAI();async function runConversation() { const inputWord = "almond latte"; const response = await openai.chat.completions.create({ messages: [ // for (let i = 0; i < message.tool_calls.length; i++) { // console.log("[CALLING]", message.tool_calls[i].function); // const tool = toolbox[message.tool_calls[i].function.name]; // if (tool) { // const result = await tool.call(JSON.parse(message.tool_calls[i].function.arguments)); // console.log("[RESULT]", truncate(result));Migrated from folder: openai_function_calling/grayWildfowlMigrated from folder: openai_function_calling/fetchWebpageMigrated from folder: openai_function_calling/weatherOfLatLonMigrated from folder: openai_function_calling/latLngOfCityMigrated from folder: openai_function_calling/weatherBotMigrated from folder: openai_function_calling/counterfeitPrompt