Alimjan
Alimjan8mo ago

using OpenAI npm in Deno deploy playground:

i don't know how to return the resualt , now this just return [object Object] i don't how to use async await this is my code: import OpenAI from "npm:openai"; const openai = new OpenAI(); openai.baseURL = "https://cn.gptapi.asia/v1" console.log(openai.baseURL) async function main() { const completion = await openai.chat.completions.create({ messages: [{ role: "system", content: "You are a helpful assistant." }], model: "gpt-3.5-turbo", }); console.log(completion.choices[0]); return completion.choices[0] } const resualt = await main(); Deno.serve((req) => new Response(resualt));
3 Replies
Deno AI Helper
Deno AI Helper8mo ago
Based on the documentation, it seems like you're trying to use the OpenAI npm package in a Deno environment and return the result of a GPT-3 completion as a server response. However, there are a few issues with your code. Firstly, the OpenAI npm package is not directly compatible with Deno. Deno does not support npm packages out of the box. You might need to find a Deno-compatible version of the OpenAI package or use a compatibility layer like deno.land/x/denofun. Secondly, the Deno.serve function is not used correctly. According to the documentation, Deno.serve is a function that creates a server and takes an object with a port property as an argument. It returns an async iterable that yields connections. You would need to use a loop to handle these connections and respond to HTTP requests. Here's an example of how you might set up a server in Deno:
import { serve } from "https://deno.land/std@0.99.0/http/server.ts";

const server = serve({ port: 8000 });

console.log("http://localhost:8000/");

for await (const req of server) {
req.respond({ body: "Hello World\n" });
}
import { serve } from "https://deno.land/std@0.99.0/http/server.ts";

const server = serve({ port: 8000 });

console.log("http://localhost:8000/");

for await (const req of server) {
req.respond({ body: "Hello World\n" });
}
In your case, you would replace "Hello World\n" with the result of your OpenAI completion. However, you need to make sure that the result is a string, as the body property of the response expects a string or Uint8Array. If completion.choices[0] is an object, you might need to use JSON.stringify to convert it to a string. Please note that this advice is not directly from the documentation and might not work as expected due to the incompatibility between npm packages and Deno. 🦕
Attacler/Bart
Attacler/Bart8mo ago
maybe try JSON.stringify ?
Alimjan
Alimjan8mo ago
thank you, i already fix this import OpenAI from "npm:openai"; const openai = new OpenAI(); openai.baseURL = "https://cn.gptapi.asia/v1" console.log(openai.baseURL) async function main(model,context,question) { const completion = await openai.chat.completions.create({ // messages: [{ role: "system", content: question }], model: model, messages: [{ role: "user", content: context }], messages: [{ role: "user", content: question }],
}); console.log(completion); return completion.choices[0].message.content } const kv = await Deno.openKv(); Deno.serve(async (req) => { if (req.body) {
const body = await req.json(); console.log("Body:", body);
const result = await main(body.model,body.context,body.question);
console.log(result); const jsonResult = JSON.stringify({result:result}); const prefs = { model:body.model, context:body.context, question:body.question, answer:result } const resultFromKV = await kv.set(["log", "alimjan",Date.now(), crypto.randomUUID()], prefs); return new Response(jsonResult, { headers: { "Content-Type": "application/json", }, }); } return new Response("Hello, World!"); });