Skip to content

Instantly share code, notes, and snippets.

@pathikrit
Created September 26, 2023 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pathikrit/241de85fb4a2be3394eabf648d6172d6 to your computer and use it in GitHub Desktop.
Save pathikrit/241de85fb4a2be3394eabf648d6172d6 to your computer and use it in GitHub Desktop.
Minimal node.js code to ask ChatGPT
import {ChatCompletionRequestMessageRoleEnum as Role, OpenAIApi} from "openai";
import {Configuration as OpenAIConfig} from "openai/dist/configuration.js";
const OPENAI_API_KEY = 'XXXXXXXXXXXXXX'
const openai = new OpenAIApi(new OpenAIConfig({apiKey: OPENAI_API_KEY}))
const ask = (query) => {
return openai.createChatCompletion({
model: 'gpt-3.5-turbo',
max_tokens: 2048,
temperature: 0.5,
messages: [
{role: Role.User, content: query}
],
functions: [
{
name: "do_web_search",
description: "Invoke this function with an appropriate search query if the answer may be outdated and needs more information from the web",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description: "Query string to trigger a web search"
}
},
required: ["query"],
}
}
]
})
.then(res => {
const message = res.data.choices[0].message
if (message.function_call) {
console.log(JSON.stringify(message.function_call))
} else {
console.log(message.content)
}
})
}
ask('What is the weather today in nyc')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment