11
12
13If you fork this, you'll need to set `OPENAI_API_KEY` in your [Val Town Secrets](https://www.val.town/settings/secrets).
14
15
2import { fetchWebpage } from "https://esm.town/v/jdan/fetchWebpage";
3import { weatherOfLatLon } from "https://esm.town/v/jdan/weatherOfLatLon";
4import { OpenAI } from "https://esm.town/v/std/openai?v=4";
5
6const openai = new OpenAI();
7
8const toolbox = {
9 "latLngOfCity": {
10 openAiTool: {
11 type: "function",
12 function: {
33 },
34 "weatherOfLatLon": {
35 openAiTool: {
36 type: "function",
37 function: {
60 },
61 "fetchWebpage": {
62 openAiTool: {
63 type: "function",
64 function: {
82};
83
84const tools = Object.values(toolbox).map(({ openAiTool }) => openAiTool);
85const transcript = [
86 { role: "user", content: "What's the weather in Hoboken, NJ? Do your best to follow URLs and summarize the weather instead of having the user do it." },
94
95async function runConversation() {
96 const response = await openai.chat.completions.create({
97 messages: transcript,
98 tools,
1Migrated from folder: openai_function_calling/fetchWebpage
1Migrated from folder: openai_function_calling/weatherOfLatLon
1Migrated from folder: openai_function_calling/latLngOfCity
1Migrated from folder: openai_function_calling/weatherBot
1/** @jsxImportSource npm:hono@3/jsx */
2import { chat } from "https://esm.town/v/stevekrouse/openai";
3import cronstrue from "npm:cronstrue";
4import { Hono } from "npm:hono@3";
1import type { ChatCompletion, ChatCompletionCreateParamsNonStreaming, Message } from "npm:@types/openai";
2
3async function getOpenAI() {
4 // if you don't have a key, use our std library version
5 if (Deno.env.get("OPENAI_API_KEY") === undefined) {
6 const { OpenAI } = await import("https://esm.town/v/std/openai");
7 return new OpenAI();
8 } else {
9 const { OpenAI } = await import("npm:openai");
10 return new OpenAI();
11 }
12}
13
14/**
15 * Initiates a chat conversation with OpenAI's GPT model and retrieves the content of the first response.
16 * This function can handle both single string inputs and arrays of message objects.
17 * It supports various GPT models, allowing for flexibility in choosing the model based on the application's needs.
25 options?: Omit<ChatCompletionCreateParamsNonStreaming, "messages">,
26): Promise<ChatCompletion & { content: string }> {
27 const openai = await getOpenAI();
28 const messages = Array.isArray(input) ? input : [{ role: "user", content: input }];
29 const createParams: ChatCompletionCreateParamsNonStreaming = {
33 messages,
34 };
35 const completion = await openai.chat.completions.create(createParams);
36
37 return { ...completion, content: completion.choices[0].message.content };
1# OpenAI ChatGPT helper function
2
3This val uses your OpenAI token if you have one, and the @std/openai if not, so it provides limited OpenAI usage for free.
4
5```ts
6import { chat } from "https://esm.town/v/stevekrouse/openai";
7
8const { content } = await chat("Hello, GPT!");
11
12```ts
13import { chat } from "https://esm.town/v/stevekrouse/openai";
14
15const { content } = await chat(
1/** @jsxImportSource npm:hono@3/jsx */
2import { OpenAI } from "https://esm.town/v/std/openai?v=2";
3import { sqlite } from "https://esm.town/v/std/sqlite?v=5";
4import { Hono } from "npm:hono@3";