generative-ai-for-beginners
generative-ai-for-beginners copied to clipboard
feat: Lesson 11 - Integrating with function calling - TypeScript sample
This PR is a TypeScript version of Lesson 11: Integrating with function calling
How to run the example?
- Create a
.envfile at the root of the application and include the information for:
AZURE_OPENAI_ENDPOINTAZURE_OPENAI_KEYBING_API_KEYBING_MAPS_BASE_URL
To create a Bing API Key you can create on: https://www.bingmapsportal.com
There is a file named .env-sample explaining how to create the .env file.
- To install dependencies, navigate to (under the lesson 11):
cd:
typescript/function-app
Then execute the command:
npm install
- After the necessary dependencies are installed in the application, open the terminal and run the command:
npm run build
- Finally, execute the command:
npm run start
- You will have the application running perfectly.
You can see in action in this gif below:
- Final result in TypeScript sample:
👋 Thanks for contributing @glaucia86! We will review the pull request and get back to you soon.
looks in general, just recommending that we add code to parse parameters, as I think that's really the value of the LLM call..
looks in general, just recommending that we add code to parse parameters, as I think that's really the value of the LLM call..
@softchris I made these changes here:
async function main() {
try {
console.log("== Chat Completions App with Functions ==");
const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
const deploymentName = "gpt-4"; // if you want to use 'gpt-4' model you need to create a resource in Sweden Central region
const userParams = {
location: "New York",
unit: "C"
};
const result = await client.getChatCompletions(deploymentName, [
{
role: "user",
content: `What's the weather in ${userParams.location}, ${userParams.unit}?`,
},
], {
functions: [getCurrentWeatherFunction],
});
for (const choice of result.choices) {
console.log(choice.message?.functionCall);
if (choice.message?.functionCall) {
const { arguments: argumentsJson } = choice.message.functionCall;
const { location, unit } = JSON.parse(argumentsJson);
let response = await findWeather(location, unit);
console.log("Result from Bing Maps API..: ", response);
}
}
} catch (error) {
console.error("The sample encountered an error...:", error);
}
}