-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
All changes required for
ai dev new openai-webpage
to work (#141)
- update c# openai client library to beta12 - added openai-chat-streaming-js template (aka `ai dev new openai-chat-streaming-js`) - added openai-functions-streaming-js template (aka `ai dev new openai-functions-streaming-js`) - added openai-webpage template (aka `ai dev new openai-webpage`) - fixed bug in `FileHelpers` where `ReadAllBytes` didn't work for resources - fixed bug in `TemplateFactory` where `ProcessTemplates` didn't work for binary files
- Loading branch information
Showing
43 changed files
with
1,455 additions
and
82 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
**/bin/*/net6.0/* | ||
**/bin/*/net7.0/* | ||
**/obj/* | ||
ideas/website/node_modules/** |
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
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
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
85 changes: 85 additions & 0 deletions
85
src/ai/.x/templates/openai-chat-streaming-js/ChatCompletionsStreaming.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<#@ template hostspecific="true" #> | ||
<#@ output extension=".js" encoding="utf-8" #> | ||
<#@ parameter type="System.String" name="OPENAI_ENDPOINT" #> | ||
<#@ parameter type="System.String" name="OPENAI_API_KEY" #> | ||
<#@ parameter type="System.String" name="AZURE_OPENAI_CHAT_DEPLOYMENT" #> | ||
<#@ parameter type="System.String" name="AZURE_OPENAI_SYSTEM_PROMPT" #> | ||
const { OpenAIClient, AzureKeyCredential } = require("@azure/openai"); | ||
|
||
class OpenAIStreamingChatCompletions { | ||
constructor(systemPrompt, endpoint, azureApiKey, deploymentName) { | ||
this.systemPrompt = systemPrompt; | ||
this.endpoint = endpoint; | ||
this.azureApiKey = azureApiKey; | ||
this.deploymentName = deploymentName; | ||
this.client = new OpenAIClient(this.endpoint, new AzureKeyCredential(this.azureApiKey)); | ||
this.clearConversation(); | ||
} | ||
|
||
clearConversation() { | ||
this.messages = [ | ||
{ role: 'system', content: this.systemPrompt } | ||
]; | ||
} | ||
|
||
async getChatCompletions(userInput, callback) { | ||
this.messages.push({ role: 'user', content: userInput }); | ||
|
||
const events = this.client.listChatCompletions(this.deploymentName, this.messages); | ||
|
||
let contentComplete = ''; | ||
for await (const event of events) { | ||
for (const choice of event.choices) { | ||
|
||
let content = choice.delta?.content; | ||
if (choice.finishReason === 'length') { | ||
content = `${content}\nERROR: Exceeded token limit!`; | ||
} | ||
|
||
if (content != null) { | ||
callback(content); | ||
await new Promise(r => setTimeout(r, 50)); // delay to simulate real-time output, word by word | ||
contentComplete += content; | ||
} | ||
} | ||
} | ||
|
||
this.messages.push({ role: 'assistant', content: contentComplete }); | ||
return contentComplete; | ||
} | ||
} | ||
|
||
const readline = require('readline'); | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}); | ||
|
||
async function main() { | ||
const endpoint = process.env["OPENAI_ENDPOINT"] || "<#= OPENAI_ENDPOINT #>"; | ||
const azureApiKey = process.env["OPENAI_API_KEY"] || "<#= OPENAI_API_KEY #>"; | ||
const deploymentName = process.env["AZURE_OPENAI_CHAT_DEPLOYMENT"] || "<#= AZURE_OPENAI_CHAT_DEPLOYMENT #>" ; | ||
const systemPrompt = process.env["AZURE_OPENAI_SYSTEM_PROMPT"] || "<#= AZURE_OPENAI_SYSTEM_PROMPT #>" ; | ||
|
||
const streamingChatCompletions = new OpenAIStreamingChatCompletions(systemPrompt, endpoint, azureApiKey, deploymentName); | ||
|
||
while (true) { | ||
|
||
const input = await new Promise(resolve => rl.question('User: ', resolve)); | ||
if (input === 'exit' || input === '') break; | ||
|
||
let response = await streamingChatCompletions.getChatCompletions(input, (content) => { | ||
console.log(`assistant-streaming: ${content}`); | ||
}); | ||
|
||
console.log(`\nAssistant: ${response}\n`); | ||
} | ||
|
||
console.log('Bye!'); | ||
} | ||
|
||
main().catch((err) => { | ||
console.error("The sample encountered an error:", err); | ||
}); | ||
|
||
module.exports = { main }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"_Name": "OpenAI Chat Completions (Streaming) in JavaScript", | ||
"_Language": "JavaScript", | ||
"OPENAI_ENDPOINT": "<insert your OpenAI endpoint here>", | ||
"OPENAI_API_KEY": "<insert your OpenAI API key here>", | ||
"AZURE_OPENAI_CHAT_DEPLOYMENT": "<insert your OpenAI deployment name here>", | ||
"AZURE_OPENAI_SYSTEM_PROMPT": "You are a helpful AI assistant." | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "openai-chat-streaming", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "ChatCompletionsStreaming.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@azure/openai": "1.0.0-beta.8" | ||
} | ||
} | ||
|
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
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
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
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
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
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
42 changes: 42 additions & 0 deletions
42
src/ai/.x/templates/openai-functions-streaming-js/ChatCompletionsCustomFunctions.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
function getCurrentWeather(function_arguments) { | ||
const location = JSON.parse(function_arguments).location; | ||
return `The weather in ${location} is 72 degrees and sunny.`; | ||
}; | ||
|
||
const getCurrentWeatherSchema = { | ||
name: "get_current_weather", | ||
description: "Get the current weather in a given location", | ||
parameters: { | ||
type: "object", | ||
properties: { | ||
location: { | ||
type: "string", | ||
description: "The city and state, e.g. San Francisco, CA", | ||
}, | ||
unit: { | ||
type: "string", | ||
enum: ["celsius", "fahrenheit"], | ||
}, | ||
}, | ||
required: ["location"], | ||
}, | ||
}; | ||
|
||
function getCurrentDate() { | ||
const date = new Date(); | ||
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`; | ||
} | ||
|
||
const getCurrentDateSchema = { | ||
name: "get_current_date", | ||
description: "Get the current date", | ||
parameters: { | ||
type: "object", | ||
properties: {}, | ||
}, | ||
}; | ||
|
||
exports.getCurrentWeather = getCurrentWeather; | ||
exports.getCurrentWeatherSchema = getCurrentWeatherSchema; | ||
exports.getCurrentDate = getCurrentDate; | ||
exports.getCurrentDateSchema = getCurrentDateSchema; |
61 changes: 61 additions & 0 deletions
61
src/ai/.x/templates/openai-functions-streaming-js/ChatCompletionsFunctionsStreaming.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const { OpenAIClient, AzureKeyCredential } = require("@azure/openai"); | ||
const { FunctionFactory } = require("./FunctionFactory"); | ||
const { FunctionCallContext } = require("./FunctionCallContext"); | ||
|
||
class ChatCompletionsFunctionsStreaming { | ||
constructor(systemPrompt, endpoint, azureApiKey, deploymentName, functionFactory) { | ||
this.systemPrompt = systemPrompt; | ||
this.endpoint = endpoint; | ||
this.azureApiKey = azureApiKey; | ||
this.deploymentName = deploymentName; | ||
this.client = new OpenAIClient(this.endpoint, new AzureKeyCredential(this.azureApiKey)); | ||
this.functionFactory = functionFactory || new FunctionFactory(); | ||
this.clearConversation(); | ||
} | ||
|
||
clearConversation() { | ||
this.messages = [ | ||
{ role: 'system', content: this.systemPrompt } | ||
]; | ||
this.functionCallContext = new FunctionCallContext(this.functionFactory, this.messages); | ||
} | ||
|
||
async getChatCompletions(userInput, callback) { | ||
this.messages.push({ role: 'user', content: userInput }); | ||
|
||
let contentComplete = ""; | ||
while (true) { | ||
const events = this.client.listChatCompletions(this.deploymentName, this.messages, { | ||
functions: this.functionFactory.getFunctionSchemas(), | ||
}); | ||
|
||
for await (const event of events) { | ||
for (const choice of event.choices) { | ||
|
||
this.functionCallContext.checkForUpdate(choice); | ||
|
||
let content = choice.delta?.content; | ||
if (choice.finishReason === 'length') { | ||
content = `${content}\nERROR: Exceeded token limit!`; | ||
} | ||
|
||
if (content != null) { | ||
callback(content); | ||
await new Promise(r => setTimeout(r, 50)); // delay to simulate real-time output, word by word | ||
contentComplete += content; | ||
} | ||
} | ||
} | ||
|
||
if (this.functionCallContext.tryCallFunction() !== undefined) { | ||
this.functionCallContext.clear(); | ||
continue; | ||
} | ||
|
||
this.messages.push({ role: 'assistant', content: contentComplete }); | ||
return contentComplete; | ||
} | ||
} | ||
} | ||
|
||
exports.ChatCompletionsFunctionsStreaming = ChatCompletionsFunctionsStreaming; |
Oops, something went wrong.