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 };
1import { OpenAI } from "https://esm.town/v/std/openai";
2
3export async function cronprompt(prompt: string) {
4 const openai = new OpenAI();
5
6 const functionExpression = await openai.chat.completions.create({
7 messages: [
8 {
2
3export const langchainEx = (async () => {
4 const { OpenAI } = await import("https://esm.sh/langchain/llms/openai");
5 const { PromptTemplate } = await import("https://esm.sh/langchain/prompts");
6 const { LLMChain } = await import("https://esm.sh/langchain/chains");
7 const model = new OpenAI({
8 temperature: 0.9,
9 openAIApiKey: process.env.openai,
10 maxTokens: 100,
11 }, {});
1import { OpenAI } from "https://esm.town/v/std/openai?v=2";
2
3const openai = new OpenAI();
4const functionExpression = await openai.chat.completions.create({
5 "messages": [
6 { "role": "user", "content": "whats the weather in sf" },
1Migrated from folder: Libraries/ai/OpenAI/openAiHelloWorld
1import { OpenAI } from "npm:openai";
2
3// Create a secret named OPENAI_API_KEY at https://www.val.town/settings/environment-variables
4
5const openai = new OpenAI();
6const functionExpression = await openai.chat.completions.create({
7 "messages": [
8 { "role": "user", "content": "Say hello in a creative way" },
1Example of using Hono to stream OpenAI's streamed chat responses!
2
3Migrated from folder: Utils/ai/examples/openAIHonoChatStreamExample
1import { fetch } from "https://esm.town/v/std/fetch";
2import { Hono } from "npm:hono";
3import { OpenAI } from "npm:openai";
4
5const gistGPT = async (input: string, about?: boolean) => {
8 const chatInput = about ? input : await (await fetch(input)).text();
9
10 const openai = new OpenAI();
11 let chatCompletion = await openai.chat.completions.create({
12 messages: [
13 {
1/** @jsxImportSource npm:hono@3/jsx */
2import { fetchText } from "https://esm.town/v/stevekrouse/fetchText";
3import { chat } from "https://esm.town/v/stevekrouse/openai";
4import cronstrue from "npm:cronstrue";
5import { Hono } from "npm:hono@3";
1import { Hono } from "npm:hono";
2import { OpenAI } from "npm:openai";
3
4const priestGPT = async (verse: string, about?: boolean) => {
5 const openai = new OpenAI();
6 let chatCompletion = await openai.chat.completions.create({
7 messages: [
8 {