Switch to og openai client

This commit is contained in:
2025-07-19 12:35:41 +03:00
parent 963ac7111f
commit dcd0ab4188
4 changed files with 28 additions and 39 deletions

8
package-lock.json generated
View File

@@ -20,7 +20,7 @@
"drizzle-orm": "^0.41.0", "drizzle-orm": "^0.41.0",
"nuxt": "^3.16.2", "nuxt": "^3.16.2",
"nuxt-auth-utils": "0.5.18", "nuxt-auth-utils": "0.5.18",
"openai": "^4.95.1", "openai": "^4.104.0",
"posthog-js": "^1.256.2", "posthog-js": "^1.256.2",
"posthog-node": "^5.1.1", "posthog-node": "^5.1.1",
"radash": "^12.1.0", "radash": "^12.1.0",
@@ -9558,9 +9558,9 @@
} }
}, },
"node_modules/openai": { "node_modules/openai": {
"version": "4.95.1", "version": "4.104.0",
"resolved": "https://registry.npmjs.org/openai/-/openai-4.95.1.tgz", "resolved": "https://registry.npmjs.org/openai/-/openai-4.104.0.tgz",
"integrity": "sha512-IqJy+ymeW+k/Wq+2YVN3693OQMMcODRtHEYOlz263MdUwnN/Dwdl9c2EXSxLLtGEHkSHAfvzpDMHI5MaWJKXjQ==", "integrity": "sha512-p99EFNsA/yX6UhVO93f5kJsDRLAg+CTA2RBqdHK4RtK8u5IJw32Hyb2dTGKbnnFmnuoBv5r7Z2CURI9sGZpSuA==",
"license": "Apache-2.0", "license": "Apache-2.0",
"dependencies": { "dependencies": {
"@types/node": "^18.11.18", "@types/node": "^18.11.18",

View File

@@ -22,7 +22,7 @@
"drizzle-orm": "^0.41.0", "drizzle-orm": "^0.41.0",
"nuxt": "^3.16.2", "nuxt": "^3.16.2",
"nuxt-auth-utils": "0.5.18", "nuxt-auth-utils": "0.5.18",
"openai": "^4.95.1", "openai": "^4.104.0",
"posthog-js": "^1.256.2", "posthog-js": "^1.256.2",
"posthog-node": "^5.1.1", "posthog-node": "^5.1.1",
"radash": "^12.1.0", "radash": "^12.1.0",

View File

@@ -150,32 +150,25 @@ export const createActivityContent = async ({
[${previousActivities.map((activity) => stringifyActivity({ activity, shouldKeepNames: true }))}] [${previousActivities.map((activity) => stringifyActivity({ activity, shouldKeepNames: true }))}]
`; `;
const [aiError, aiResponse] = await openai("/responses", { const aiResponse = await openai.responses.create({
body: { model: "gpt-4o-mini",
model: "gpt-4.1", input: [{ role: "user", content: prompt }],
input: [ text: {
{ format: {
role: "user", type: "json_schema",
content: prompt, name: "activity",
}, schema: {
], type: "object",
text: { properties: {
format: { title: {
type: "json_schema", type: "string",
name: "activity", },
schema: { description: {
type: "object", type: "string",
properties: {
title: {
type: "string",
},
description: {
type: "string",
},
}, },
required: ["title", "description"],
additionalProperties: false,
}, },
required: ["title", "description"],
additionalProperties: false,
}, },
}, },
}, },
@@ -183,7 +176,7 @@ export const createActivityContent = async ({
const [parseError, responseObject] = tryit( const [parseError, responseObject] = tryit(
chain( chain(
(r) => get(r, "output.0.content.0.text"), (r) => get(r, "output_text"),
(r) => safeDestr<{ title: string; description: string }>(r), (r) => safeDestr<{ title: string; description: string }>(r),
), ),
)(aiResponse); )(aiResponse);
@@ -197,5 +190,5 @@ export const createActivityContent = async ({
}, },
}; };
return [aiError || parseError, stravaRequestBody] as const; return [parseError, stravaRequestBody] as const;
}; };

View File

@@ -1,15 +1,11 @@
import { tryit } from "radash"; import OpenAI from "openai";
export const useOpenAI = () => { export const useOpenAI = () => {
const config = useRuntimeConfig(); const config = useRuntimeConfig();
const client = $fetch.create({ const client = new OpenAI({
baseURL: "https://api.openai.com/v1", apiKey: config.openaiApiKey,
headers: {
Authorization: `Bearer ${config.openaiApiKey}`,
},
method: "post",
}); });
return tryit(client); return client;
}; };