Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

azure gpt api stream response

$
0
0

I want to stream responce from gpt api in node.js. I can stream response from OpenAI GPT api using this code:

import OpenAI from "openai";const openai = new OpenAI({    apiKey: 'my_api_key',});const response = await openai.chat.completions.create({    model: "gpt-3.5-turbo",    messages: [        {"role": "user","content": "Generate Lorem Ipsum text."        }    ],    temperature: 0,    stream: true});for await (const chunk of response[Symbol.asyncIterator]()) {    try {        process.stdout.write(chunk['choices'][0]['delta']['content']);    } catch (err) {    }}

How can I rewrite this code using azure? I tried something like this, but does not work:

const {OpenAIClient, AzureKeyCredential} = require("@azure/openai");const endpoint = 'my_endpoint';const azureApiKey = 'my_azure_gpt_api_key';const messages = [    {"role": "user","content": "Generate Lorem Ipsum text."    }];async function main() {    const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));    const deploymentId = "gpt35-turbo-deploy";    const response = await client.getChatCompletions(deploymentId, messages, {        temperature: 0,        stream: true    })    // TODO console.log(response['choices'][0]['message']['content']) how?}main().catch((err) => {    console.error("The sample encountered an error:", err);});module.exports = {main};

Should I use library mentioned above? Or can I use something different?


Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>