1import { Hono } from "npm:hono";
2import { OpenAI } from "npm:openai";
3
4const defaultUser = {
95
96const personalizationGPT = async (user: UserObject) => {
97 const openai = new OpenAI();
98 let chatCompletion = await openai.chat.completions.create({
99 messages: [
100 {
5Use GPT to return JIT personalization for client side applications.
6
7If you fork this, you'll need to set `OPENAI_API_KEY` in your [Val Town Secrets](https://www.val.town/settings/secrets).
8
9Migrated from folder: PersonalizationGPT/PersonalizationGPT
3import { jsx } from "npm:hono@3/jsx";
4import { cors } from 'npm:hono/cors';
5import { OpenAI } from "npm:openai";
6
7const app = new Hono();
8const openai = new OpenAI();
9
10app.use('*', cors({
19 <html>
20 <head>
21 <title>OpenAI Prompt Example</title>
22 <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
23 <script src="https://cdn.tailwindcss.com"></script>
25 <body>
26 <div class="container mx-auto py-8">
27 <h1 class="text-4xl font-bold mb-4">OpenAI Prompt Example</h1>
28 <form action="/prompt" method="GET">
29 <label for="prompt" class="block mb-2 font-bold">Prompt:</label>
50
51 try {
52 const response = await openai.chat.completions.create({
53 model: "gpt-4",
54 messages: [{ role: "user", content: prompt }],
59 return c.redirect(`/?response=${encodeURIComponent(generatedResponse)}`);
60 } catch (error) {
61 console.error('OpenAI API error:', error);
62 return c.redirect('/?response=Error%20occurred.');
63 }
1Migrated from folder: Libraries/ai/OpenAI/openAiExample
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(
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