Skip to content

Enhance Chat App Basics documentation with clearer instructions #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions 03-CoreGenerativeAITechniques/01-lm-completions-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,29 @@ Let's see how you would use text completions using the **Microsoft.Extensions.AI

> πŸ§‘β€πŸ’»**Sample code**: [Here is a working example of this application](./src/BasicChat-01MEAI/) you can follow along with.

#### How to run the sample code

To run the sample code, you'll need to:

1. Make sure you have set up a GitHub Codespace with the appropriate environment as described in the [Setup guide](../02-SetupDevEnvironment/readme.md)
2. Ensure you have configured your GitHub Token as described in the [Pre-flight check section](../02-SetupDevEnvironment/readme.md#pre-flight-check-setting-up-github-access-tokens)
3. Open a terminal in your codespace (Ctrl+` or Cmd+`)
4. Navigate to the sample code directory:
```bash
cd 03-CoreGenerativeAITechniques/src/BasicChat-01MEAI
```
5. Run the application:
```bash
dotnet run
```

```csharp

// this example illustrates using a model hosted on GitHub Models
IChatClient client = new ChatCompletionsClient(
endpoint: new Uri("https://models.inference.ai.azure.com"),
new AzureKeyCredential(githubToken)) // githubToken is retrieved from the environment variables
.AsChatClient("gpt-4o-mini");
.AsIChatClient("Phi-3.5-MoE-instruct");

// here we're building the prompt
StringBuilder prompt = new StringBuilder();
Expand All @@ -39,16 +55,18 @@ prompt.AppendLine("I found this product based on the other reviews. It worked fo
// send the prompt to the model and wait for the text completion
var response = await client.GetResponseAsync(prompt.ToString());

// display the repsonse
Console.WriteLine(response.Message);
// display the response
Console.WriteLine(response.Text);

```

> πŸ—’οΈ**Note:** This example showed GitHub Models as the hosting service. If you want to use Ollama, [check out this example](./src/BasicChat-03Ollama/) (it instantiates a different `IChatClient`).
>
> If you want to use Azure AI Foundry you can use the same code, but you will need to change the endpoint and the credentials.
>
> For instructions on how to set up Ollama, refer to [Getting Started with Ollama](../02-SetupDevEnvironment/getting-started-ollama.md).

> πŸ™‹ **Need help?**: If you encounter any issues, [open an issue in the repository](https://github.com/microsoft/Generative-AI-for-beginners-dotnet/issues/new).
> πŸ™‹ **Need help?**: If you encounter any issues running this example, [open an issue in the repository](https://github.com/microsoft/Generative-AI-for-beginners-dotnet/issues/new?template=Blank+issue) and we'll help you troubleshoot.

### Chat applications

Expand All @@ -68,6 +86,8 @@ During the chat with the model, you will need to keep track of the chat history.

Let's take a look at how you would build a chat application using MEAI.

> πŸ§‘β€πŸ’»**Sample code**: You can find complete chat application examples in the [BasicChat-01MEAI](./src/BasicChat-01MEAI/) and [BasicChat-02SK](./src/BasicChat-02SK/) directories.

```csharp

// assume IChatClient is instantiated as before
Expand Down Expand Up @@ -102,7 +122,7 @@ while (true)

> πŸ—’οΈ**Note:** This can also be done with Semantic Kernel. [Check out the code here](./src/BasicChat-02SK/).

> πŸ™‹ **Need help?**: If you encounter any issues, [open an issue in the repository](https://github.com/microsoft/Generative-AI-for-beginners-dotnet/issues/new).
> πŸ™‹ **Need help?**: If you encounter any issues running the chat application examples, [open an issue in the repository](https://github.com/microsoft/Generative-AI-for-beginners-dotnet/issues/new?template=Blank+issue) and we'll help you troubleshoot.

## Function calling

Expand All @@ -118,6 +138,8 @@ When building AI applications you are not limited to just text-based interaction

There are a couple of setup steps you need to take in order to call functions with MEAI.

> πŸ§‘β€πŸ’»**Sample code**: [Here is a working example of function calling](./src/MEAIFunctions/) you can follow along with. To run this example, follow the same steps as for the previous examples, but navigate to `03-CoreGenerativeAITechniques/src/MEAIFunctions` directory.

1. First, of course, define the function that you want the chatbot to be able to call. In this example we're going to get the weather forecast.

```csharp
Expand Down Expand Up @@ -151,7 +173,7 @@ There are a couple of setup steps you need to take in order to call functions wi
IChatClient client = new ChatCompletionsClient(
endpoint: new Uri("https://models.inference.ai.azure.com"),
new AzureKeyCredential(githubToken)) // githubToken is retrieved from the environment variables
.AsChatClient("gpt-4o-mini")
.AsIChatClient("gpt-4o-mini")
.AsBuilder()
.UseFunctionInvocation() // here we're saying that we could be invoking functions!
.Build();
Expand All @@ -160,12 +182,13 @@ There are a couple of setup steps you need to take in order to call functions wi
1. Then finally when we interact with the model, we'll send the `ChatOptions` object that specifies the function the model could call if it needs to get the weather info.

```csharp
var responseOne = await client.GetResponseAsync("What is today's date", chatOptions); // won't call the function

var responseTwo = await client.GetResponseAsync("Should I bring an umbrella with me today?", chatOptions); // will call the function
var question = "Do I need an umbrella today?";
Console.WriteLine($"question: {question}");
var response = await client.GetResponseAsync(question, chatOptions);
Console.WriteLine($"response: {response}");
```

> πŸ™‹ **Need help?**: If you encounter any issues, [open an issue in the repository](https://github.com/microsoft/Generative-AI-for-beginners-dotnet/issues/new).
> πŸ™‹ **Need help?**: If you encounter any issues with function calling, [open an issue in the repository](https://github.com/microsoft/Generative-AI-for-beginners-dotnet/issues/new?template=Blank+issue) and we'll help you troubleshoot.

## Summary

Expand Down
Loading