Created
September 26, 2023 18:56
-
-
Save pathikrit/241de85fb4a2be3394eabf648d6172d6 to your computer and use it in GitHub Desktop.
Minimal node.js code to ask ChatGPT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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