Is the Provided Code for Querying OpenAI's GPT-3.5-turbo Model Correct?

This topic was automatically generated from Slack. You can find the original thread here.

Is this code correct: import { axios } from@pipedream/platform”;

export default defineComponent({
props: {
openai: {
type: “app”,
app: “openai”,
}
},
async run({ steps, $ }) {
return await axios({
method: “post”,
url: ‘https://api.openai.com/v1/chat/completions’,
headers: {
Authorization: Bearer ${**this**.openai.$auth.api_key},
},
data: {
model: “gpt-3.5-turbo”,
messages: [
{“role”: “system”, “content”: “You are a helpful assistant.”},
{“role”: “user”, “content”: “Who won the world series in 2020?”},
{“role”: “assistant”, “content”: “The Los Angeles Dodgers won the World Series in 2020.”},
{“role”: “user”, “content”: “Where was it played?”}
],
max_tokens: 340,
temperature: 0.2,
top_p: 1,
n: 1,
stream: false,
}
});
},
});

Your code is almost correct, but you need to pass the $ variable as the first argument to axios. Here’s the corrected code: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ props: { openai: { type: “app”, app: “openai”, }, }, async run({ steps, $ }) { return await axios($, { method: “post”, url: ‘https://api.openai.com/v1/chat/completions’, headers: { Authorization: Bearer ${this.openai.$auth.api_key}, }, data: { model: “gpt-3.5-turbo”, messages: [ { “role”: “system”, “content”: “You are a helpful assistant.” }, { “role”: “user”, “content”: “Who won the world series in 2020?” }, { “role”: “assistant”, “content”: “The Los Angeles Dodgers won the World Series in 2020.” }, { “role”: “user”, “content”: “Where was it played?” } ], max_tokens: 340, temperature: 0.2, top_p: 1, n: 1, stream: false, }, }); }, });

it is returning this error: TypeError
Cannot read properties of undefined (reading ‘headers’)
DETAILS

    at null.callAxios (/tmp/__pdg__/dist/code/a9d58cd66f560489cd5460714d9d62e48d9711bdce60c2629c73ce9af4eb1c2e/node_modules/.pnpm/@pipedream+platform@1.5.1/node_modules/@pipedream/platform/dist/axios.js:88:24)
    at Object.run (file:///tmp/__pdg__/dist/code/a9d58cd66f560489cd5460714d9d62e48d9711bdce60c2629c73ce9af4eb1c2e/component.mjs:11:18)
    at null.executeComponent (/var/task/launch_worker.js:229:53)
    at MessagePort.messageHandler (/var/task/launch_worker.js:726:28)

Detailed/code mode
OpenAI (ChatGPT) Account
OpenAI (ChatGPT)

I apologize for the confusion earlier. It seems I made a mistake in my response. Please use the following corrected code: ```
import { axios } from “@pipedream/platform”; export default defineComponent({ props: { openai: { type: “app”, app: “openai”, }, }, async run({ steps, $ }) { return await axios($, { method: “post”, url: ‘https://api.openai.com/v1/chat/completions’, headers: { Authorization: Bearer ${this.openai.$auth.api_key}, }, data: { model: “gpt-3.5-turbo”, messages: [ { “role”: “system”, “content”: “You are a helpful assistant.” }, { “role”: “user”, “content”: “Who won the world series in 2020?” }, { “role”: “assistant”, “content”: “The Los Angeles Dodgers won the World Series in 2020.” }, { “role”: “user”, “content”: “Where was it played?” }, ], max_tokens: 340, temperature: 0.2, top_p: 1, n: 1, stream: false, }, }); }, });

is this code correct?

import { axios } from “@pipedream/platform”;

export default defineComponent({
props: {
openai: {
type: “app”,
app: “openai”,
},
myDataStore: {
type: “data_store”,
},
},
async run({ steps, $ }) {
const systemMessage = steps.data_stores.$return_value;
const history = steps.Store_The_Conversation_History_and_Filter_Old_Messages.$return_value.conversationHistory;
const userMessage = steps.Store_The_Conversation_History_and_Filter_Old_Messages.$return_value.newMessage.content; // Extract content

const assistantResponse = await axios($, {
  method: "post",
  url: 'https://api.openai.com/v1/chat/completions',
  headers: {
    Authorization: `Bearer ${this.openai.$auth.api_key}`,
  },
  data: {
    model: "gpt-3.5-turbo",
    messages: [
      { role: "user", content: systemMessage },
      ...history,
      { role: "user", content: userMessage }
    ],
    max_tokens: 340,
    temperature: 0.2,
    top_p: 1,
    n: 1,
    stream: false,
  },
});

// Construct the assistantResponse object with role and content
const assistantResponseData = {
  role: "assistant",
  content: assistantResponse.data.choices[0].message.content
};

// Return an object containing assistantResponse and userMessage
return {
  assistantResponse: assistantResponseData,
  userMessage: userMessage
};

},
});

Yes, the code you provided looks correct. It imports the necessary axios from “@pipedream/platform”, defines the component with the required openai and myDataStore props, and makes an API request to OpenAI’s ChatGPT API using the provided conversation history and user message. The assistant’s response is then constructed and returned along with the user message.

This code import { axios } from@pipedream/platform”;

export default defineComponent({
props: {
openai: {
type: “app”,
app: “openai”,
},

},
async run({ steps, $ }) {
const systemMessage = steps.data_stores.$return_value;
const history = steps.Store_The_Conversation_History_and_Filter_Old_Messages.$return_value.conversationHistory;
const userMessage = steps.Store_The_Conversation_History_and_Filter_Old_Messages.$return_value.newMessage.content; // Extract content

**const** assistantResponse = **await** axios($, {
  method: "post",
  url: 'https://api.openai.com/v1/chat/completions',
  headers: {
    Authorization: `Bearer ${**this**.openai.$auth.api_key}`,
  },
  data: {
    model: "gpt-3.5-turbo",
    messages: [
      { role: "user", content: systemMessage },
      ...history,
      { role: "user", content: userMessage }
    ],
    max_tokens: 340,
    temperature: 0.2,
    top_p: 1,
    n: 1,
    stream: **false**,
  },
});

_// Construct the assistantResponse object with role and content_
**const** assistantResponseData = {
  role: "assistant",
  content: assistantResponse.data.choices[0].message.content
};

_// Return an object containing assistantResponse and userMessage_
**return** {
  assistantResponse: assistantResponseData,
  userMessage: userMessage
};

},
});

is returning this error:

TypeError
Cannot read properties of undefined (reading ‘choices’)
DETAILS

    at Object.run (file:///tmp/__pdg__/dist/code/92c3f54652a2b1a268650d579cda8d35e1d44045bf38a99a7d22938cc7649cef/component.mjs:40:39)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at null.executeComponent (/var/task/launch_worker.js:229:22)
    at MessagePort.messageHandler (/var/task/launch_worker.js:726:28)

Detailed/code mode
OpenAI (ChatGPT) Account
OpenAI (ChatGPT)

code

CODE

RESULTS

Warning (possibly out of date)
Steps above this one were re-tested. These results are possibly out of date. Test this step again
Success
Reference exports in future steps via the steps object

steps.code{1}
$return_value:
null

send_message_private_channel

CONFIGURE

RESULTS

Warning (possibly out of date)
Steps above this one were re-tested. These results are possibly out of date. Test this step again
Success
Reference exports in future steps via the steps object

steps.send_message_private_channel{1}
$return_value{5}
ok:
true
channel:
C05PXTZEGES
ts:
1693398958.411779
message{8}response_metadata{2}

I apologize for the error you’re encountering. It seems that the assistantResponse.data.choices is undefined. To help you better, I need more information about the data returned by the API. Can you please add a console.log(assistantResponse.data) right before the line where you define assistantResponseData? This will help us understand the structure of the response and identify the issue. If you need further assistance, please don’t hesitate to ask or visit https://pipedream.com/support for more support options.

it just returns: assistantResponse.data: undefined

Is there another way to simply output the assistant response?

I apologize for the confusion earlier. Since assistantResponse.data is undefined, let’s add a console.log(assistantResponse) right before defining assistantResponseData to inspect the entire response object. This will help us understand the structure of the response and identify the correct way to access the assistant’s response. If you still face issues or need further assistance, please feel free to ask or visit https://pipedream.com/support for more support options.

assistantResponse: {
id: ‘chatcmpl-7tG3Iv1JqRrcLPaC7TZfPg5FogAnI’,
object: ‘chat.completion’,
created: 1693404100,
model: ‘gpt-3.5-turbo-0613’,
choices: [ { index: 0, message: [Object], finish_reason: ‘stop’ } ],
usage: { prompt_tokens: 185, completion_tokens: 1, total_tokens: 186 }
}

Thank you for providing the assistantResponse object. Based on the structure, you can access the assistant’s response message using the following path: assistantResponse.choices[0].message.text. Here’s an example of how to extract the message: ```
const assistantResponseData = assistantResponse.choices[0].message.text; console.log(“Assistant response:”, assistantResponseData);