#Property 'data' does not exist on type 'Response'.

13 messages · Page 1 of 1 (latest)

analog rapids
#

So im using this demo code to try to learn functions in gpt API, and data doesnt seem to exist on completion. Every tutorial I follow shows this. Is there anyway to get this to work?

// IMPORTANT! Set the runtime to edge
export const runtime = "edge";

export async function POST(req: Request) {

const { messages } = await req.json();

async function lookupTime(location: any) {
try {
const response = await fetch(
http://worldtimeapi.org/api/timezone/${location}
); // Make a GET request to the World Time API with the location parameter as the timezone.
const { datetime } = await response.json(); // Extract the datetime property from the data in the response.
const dateTime = moment(datetime, location).format("h:mmA"); // Use moment-timezone to create the Date object in the specified timezone.
console.log(The current time in ${location} is ${dateTime}.); // Log the formatted time to the console.
} catch (error) {
console.error(error); // Log any errors that occur to the console.
}
}

// Ask OpenAI for a streaming chat completion given the prompt
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo-0613",
stream: true,
messages,
functions: [
{
name: "lookupTime",
description: "get the current time in a given location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description:
"The location, e.g. Beijing, China. But it should be written in a timezone name like Asia/Shanghai",
},
},
required: ["location"], // specify that the location parameter is required
},
},
],
function_call: "auto",
});

// Extract the generated completion from the OpenAI API response.
const completionResponse = completion.data.choices[0].message;
console.log(completionResponse);

winged wren
upbeat gulch
#

There is no "data" as far as I know. Here is my code (in Python).

response = openai.ChatCompletion.create(
             model=self.model,
             messages=self.messages,
             functions=self.functions)
answer = response['choices'][0]['message']
function_call = answer.get('function_call')
analog rapids
# winged wren Is that JS? What library are you using? You can also inspect the completion obje...

Yes that is JS. It’s weird because I watched a lot of videos on it and they use data. I just can’t figure out of how.

Example response would be something like this

{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?",
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}

analog rapids
winged wren
analog rapids
# winged wren Did you check the object fields in the debugger as I suggested?

Yes and I made a little bit of a breakthru. Im using openai-edge package not just the regular open AI package because im using it with vercels AI SDK. My issue now is I cant parse the response.

But this line fixed the type issue

    (await completion.json()) as ResponseTypes["createChatCompletion"];
  const completionResponse = completionData.choices[0].message;```
winged wren
#

I'm not using JS, but good to know that you handled it.👍

analog rapids
#

Thank you for helping!

#

But u were also right, I am not using the one in the docs. Im using the openai-edge package, which supports streaming, but its creating some roadblocks for me because its making the response inaccessible in its current type

slate vector
#

Streaming does not return the standard JSON, It returns the JSON in SSE format which you would have to access as such or convers to standard JSON.

To look at the difference print the raw JSON so you can see the structure of your responses instead of trying to access a key in a malformed JSON reply.

analog rapids
#

Yes so I see that when I disable streaming it returns a normal JSON, and when streaming is on is sends each word in chunks. Also, when I try to access completions in multiple places, it gives me error Error [TypeError]: ReadableStream.prototype.pipeThrough cannot be used on a locked ReadableStream at ReadableStream.pipeThrough (file:///Users/salvatorenoto/Documents/Official/fileunit/fileunit-official/node_modules/.pnpm/next@13.4.9_@babel+core@7.22.8_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/compiled/edge-runtime/index.js:1:970334) at AIStream (webpack-internal:///(sc_server)/./node_modules/.pnpm/ai@2.1.17_react@18.2.0_svelte@4.0.5_vue@3.3.4/node_modules/ai/dist/index.mjs:123:31) at OpenAIStream (webpack-internal:///(sc_server)/./node_modules/.pnpm/ai@2.1.17_react@18.2.0_svelte@4.0.5_vue@3.3.4/node_modules/ai/dist/index.mjs:152:12) at POST (webpack-internal:///(sc_server)/./app/api/chat/route.ts:73:68) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async eval (webpack-internal:///(sc_server)/./node_modules/.pnpm/next@13.4.9_@babel+core@7.22.8_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:213:37) { digest: undefined }