Search

3,376 results found for openai (1987ms)

Code
3,281

// Telegram Bot that uses OpenAI's DALL-E to generate images
import { OpenAI } from "https://esm.town/v/std/openai";
// Initialize OpenAI client
const openai = new OpenAI();
// Telegram Bot API types
}
// Function to generate an image using OpenAI's DALL-E
async function generateImage(prompt: string) {
try {
const response = await openai.images.generate({
model: "dall-e-3",
prompt: prompt,
chatId,
"👋 Welcome to the Image Generation Bot!\n\n" +
"I can generate images based on your descriptions using OpenAI's DALL-E model.\n\n" +
"Simply send me a description of the image you want to create, and I'll generate it for
"For example: *A serene lake surrounded by mountains at sunset*"
Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to cre
### OpenAI
```ts
import { OpenAI } from "https://esm.town/v/std/openai";
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
messages: [
{ role: "user", content: "Say hello in a creative way" },
// Val Town Narrative Engine with Integrated Test UI
// =============================================================================
import { OpenAI } from "https://esm.town/v/std/openai"; // Val Town's OpenAI integration
// import { Env } from "https://esm.town/v/std/env"; // Not needed if OpenAI() handles API key
// --- Configuration & Constants ---
const OPENAI_MODEL = "gpt-4o";
const MAX_TOKENS_RESPONSE = 700;
}
// --- OpenAI System Prompt (same as before) ---
const SYSTEM_PROMPT = `
You are a Rick and Morty-styled transdimensional narrative engine, probably cobbled together by
} = ensureDefaultsAndInferStates(inputContext);
const contextForOpenAI = {
...processedContext,
_inferred_states_for_your_convenience: {
};
const userMessage = `Input Context JSON (with inferred states for your reference):\n${
JSON.stringify(contextForOpenAI, null, 2)
}`;
try {
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
model: OPENAI_MODEL,
response_format: { type: "json_object" },
messages: [
const rawContent = completion.choices[0]?.message?.content;
if (!rawContent) {
console.error("OpenAI returned an empty response message.");
return new Response(
JSON.stringify(
getFallbackOutput("OpenAI empty response.", processedContext),
),
{ status: 500, headers: { "Content-Type": "application/json" } },
) {
console.error(
"OpenAI response missing critical fields or incorrect structure.",
JSON.stringify(generatedJson, null, 2),
);
JSON.stringify(
getFallbackOutput(
"OpenAI response schema validation failed after generation.",
processedContext,
),
});
} catch (error) {
console.error("Error during OpenAI API call or processing:", error);
let errorMessage = "Internal server error";
nse.data && error.response.data.error) { // More specific OpenAI error
errorMessage = `OpenAI API Error: ${error.response.data.error.message}`;
} else if (error.message) {
errorMessage = error.message;
join/us/main.ts
10 matches
import { fetch } from "https://esm.town/v/std/fetch";
import { OpenAI } from "https://esm.town/v/std/openai";
import { z } from "npm:zod";
}
async function callOpenAI(sysP: string, userP: string, mid: string, tid: string, log: LogFn): Pr
log("DB", "OpenAI", `Call tid=${tid}`, { sL: sysP.length, uL: userP.length }, mid, tid);
try { // @ts-ignore
const oai = new OpenAI();
const comp = await oai.chat.completions.create({
model: "gpt-4o-mini",
const usg = comp.usage;
if (!resT) {
log("WN", "OpenAI", `No text tid=${tid}.`, { usg, fin: comp.choices[0]?.finish_reason }, m
return null;
}
log("IN", "OpenAI", `OK tid=${tid}`, { rL: resT.length, usg, fin: comp.choices[0]?.finish_re
return resT.trim();
} catch (err: any) {
st: err.status,
};
log("ER", "OpenAI", `Fail tid=${tid}:${err.message}`, { e: eD }, mid, tid);
throw new Error(
`OpenAI Fail: ${err.message}` + (err.code ? `(C:${err.code},S:${err.status})` : `(S:${err.
);
}
return { mid, cid: tid, p: {} as TOD, e: `${aC.name} fail:PromptGenErr.` };
}
const rOR = await callOpenAI(sP, uP, mid, tid, l);
if (!rOR) {
l("WN", aC.name, `OpenAI no content tid=${tid}.`, 0, mid, tid);
return { mid, cid: tid, p: {} as TOD, e: `${aC.name} fail:AI no content.` };
}
import { OpenAI } from "https://esm.town/v/std/openai";
// Interface for formatting rules
export async function extractFlightInfoWithAI(event: any): Promise<string> {
try {
const openai = new OpenAI();
const prompt = `
`;
const completion = await openai.chat.completions.create({
messages: [
{ role: "system", content: "You extract airport codes from flight information." },
<a href="?q=function" className="example-link">function</a>
<a href="?q=discord" className="example-link">discord</a>
<a href="?q=openai" className="example-link">openai</a>
<a href="?q=react" className="example-link">react</a>
</div>
<a href="?q=function" className="example-link">function</a>
<a href="?q=discord" className="example-link">discord</a>
<a href="?q=openai" className="example-link">openai</a>
<a href="?q=react" className="example-link">react</a>
</div>
},
{
"title": "An Introduction to OpenAI fine-tuning",
"slug": "an-introduction-to-openai-fine-tuning",
"link": "/blog/an-introduction-to-openai-fine-tuning",
"description": "How to customize OpenAI to your liking",
"pubDate": "Fri, 25 Aug 2023 00:00:00 GMT",
"author": "Steve Krouse",
"slug": "val-town-newsletter-16",
"link": "/blog/val-town-newsletter-16",
"description": "Our seed round, growing team, Codeium completions, @std/openai, and more",
"pubDate": "Mon, 22 Apr 2024 00:00:00 GMT",
"author": "Steve Krouse",
- Maintains context within conversations
- Generates images based on text descriptions
- Analyzes uploaded images using OpenAI's vision capabilities
- Simple rule-based response system
### Backend
- Built with Hono for routing
- OpenAI integration for image analysis
- Val Town image generation service integration
- Conversation context tracking with support for different message types
### Image Analysis
- Uses OpenAI's vision capabilities to analyze uploaded images
- Supports common image formats (JPEG, PNG, etc.)
- Provides detailed descriptions of image content
// For image analysis
import { OpenAI } from "https://esm.town/v/std/openai";
const app = new Hono();
const openai = new OpenAI();
// Unwrap Hono errors to see original error details
}
// Image analysis function using OpenAI
async function analyzeImage(imageData: string) {
try {
const base64Image = imageData.split(',')[1];
// Call OpenAI API to analyze the image
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
Note: When changing a SQLite table's schema, change the table's name (e.g., add _2 or _3) to cre
### OpenAI
```ts
import { OpenAI } from "https://esm.town/v/std/openai";
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
messages: [
{ role: "user", content: "Say hello in a creative way" },