Calling Azure OpenAI ChatGPT model with openai packages in node.js
Azure OpenAI, ChatGPT nodejs

Calling Azure OpenAI ChatGPT model with openai packages in node.js

Here is the Azure OpenAI ChatGPT Node.js code sample with small trick!

The Conversations Object format

conversation = {
 ?"past_user_inputs":[
? ? ? "Hi",
? ? ? "How old are you?"
? ?],
? ?"generated_responses":[
? ? ? "I am good!",
? ? ? "I am in my twenties. How about you? What do you do for a living?"
? ?],
? ?"text":"I am teacher and how about you?"
};        

The Azure OpenAI ChatGPT code, and the main difference is to change the header and includes model name in the path.

const { Configuration, OpenAIApi } = require("openai");

function messageCombiner(a, b) {
? return a.map((k, i) => ({ sender: k, text: b[i] }));
}

function createPrompt(system_message, messages) {
? let prompt = system_message;
? for (const message of messages) {
? ? prompt += `\n<|im_start|>${message.sender}\n${message.text}\n<|im_end|>`;
? }
? prompt += "\n<|im_start|>assistant\n";
? return prompt;
}

async function azureOpenAiChatGPT(conversation, model) {
? const messages = messageCombiner(conversation.past_user_inputs, conversation.generated_responses);
? const systemMessage = "<|im_start|>system\n I am assistant.\n<|im_end|>"
? messages.push({ sender: "user", text: conversation.text });

? const configuration = new Configuration({
? ? basePath: process.env.basePath + model,
? });
? const openai = new OpenAIApi(configuration);
? try {
? ? const completion = await openai.createCompletion({
? ? ? prompt: createPrompt(systemMessage, messages),
? ? ? max_tokens: 800,
? ? ? temperature: 0.7,
? ? ? frequency_penalty: 0,
? ? ? presence_penalty: 0,
? ? ? top_p: 0.95,
? ? ? stop: ["<|im_end|>"]
? ? }, {
? ? ? headers: {
? ? ? ? 'api-key': process.env.apikey,
? ? ? },
? ? ? params: { "api-version": "2022-12-01" }
? ? });
? ? return completion.data.choices[0].text;
? } catch (e) {
? ? console.error(e);
? ? return "";
? }
}        

You need to deploy your model and copy from

basePath="https://cyruswong.openai.azure.com/openai/deployments/"
model="gpt-35-tubo"        
No alt text provided for this image

The official sample is in Python.

Wong Chun Yin,Cyrus (黃俊彥) great overview and guidance on how to use Azure OpenAI Services!

要查看或添加评论,请登录

社区洞察

其他会员也浏览了