generative-ai-for-beginners icon indicating copy to clipboard operation
generative-ai-for-beginners copied to clipboard

feat: Lesson 11 - Integrating with function calling - TypeScript sample

Open glaucia86 opened this issue 1 year ago • 3 comments

This PR is a TypeScript version of Lesson 11: Integrating with function calling

How to run the example?

  1. Create a .env file at the root of the application and include the information for:
  • AZURE_OPENAI_ENDPOINT
  • AZURE_OPENAI_KEY
  • BING_API_KEY
  • BING_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.

  1. To install dependencies, navigate to (under the lesson 11):

cd: typescript/function-app

Then execute the command:

npm install

  1. After the necessary dependencies are installed in the application, open the terminal and run the command:

npm run build

  1. Finally, execute the command:

npm run start

  1. You will have the application running perfectly.

You can see in action in this gif below:

  • Final result in TypeScript sample:

lesson-11-functions-app

glaucia86 avatar Jan 19 '24 01:01 glaucia86

👋 Thanks for contributing @glaucia86! We will review the pull request and get back to you soon.

github-actions[bot] avatar Jan 19 '24 01:01 github-actions[bot]

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 avatar Jan 19 '24 20:01 softchris

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);
  }
}

glaucia86 avatar Jan 19 '24 20:01 glaucia86