Skip to content

Commit 1eec77c

Browse files
authored
Merge pull request #46 from microsoft/bruno-add-ollama-functioncalling-sample
Add Function Calling sample using Ollama
2 parents 9ed430b + 0dbcec2 commit 1eec77c

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

03-CoreGenerativeAITechniques/src/BasicChat-03Ollama/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.AI;
2+
using System.Text;
23

34
IChatClient client =
45
new OllamaChatClient(new Uri("http://localhost:11434/"), "llama3.2-vision");

03-CoreGenerativeAITechniques/src/CoreGenerativeAITechniques.sln

+7
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RAGSimple-10SKOllama", "RAG
5151
EndProject
5252
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MEAIFunctions", "MEAIFunctions\MEAIFunctions.csproj", "{33CE094D-6C79-4151-87D5-8D49A9982328}"
5353
EndProject
54+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MEAIFunctionsOllama", "MEAIFunctionsOllama\MEAIFunctionsOllama.csproj", "{B0A1DF75-4625-45D5-87B0-29F7290F63E1}"
55+
EndProject
5456
Global
5557
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5658
Debug|Any CPU = Debug|Any CPU
@@ -129,6 +131,10 @@ Global
129131
{33CE094D-6C79-4151-87D5-8D49A9982328}.Debug|Any CPU.Build.0 = Debug|Any CPU
130132
{33CE094D-6C79-4151-87D5-8D49A9982328}.Release|Any CPU.ActiveCfg = Release|Any CPU
131133
{33CE094D-6C79-4151-87D5-8D49A9982328}.Release|Any CPU.Build.0 = Release|Any CPU
134+
{B0A1DF75-4625-45D5-87B0-29F7290F63E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
135+
{B0A1DF75-4625-45D5-87B0-29F7290F63E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
136+
{B0A1DF75-4625-45D5-87B0-29F7290F63E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
137+
{B0A1DF75-4625-45D5-87B0-29F7290F63E1}.Release|Any CPU.Build.0 = Release|Any CPU
132138
EndGlobalSection
133139
GlobalSection(SolutionProperties) = preSolution
134140
HideSolutionNode = FALSE
@@ -152,6 +158,7 @@ Global
152158
{DA9468F0-A153-4023-A6F8-ADFCEFECABD9} = {767605A1-DA28-413C-BFBA-6F5C8809E11F}
153159
{35DF7A10-DE5D-41CA-9940-F93FA1385534} = {B5980212-D3CF-4A46-94EA-3B22EFAD1257}
154160
{33CE094D-6C79-4151-87D5-8D49A9982328} = {F5798C6F-98D6-4411-A5F0-F928CBA64095}
161+
{B0A1DF75-4625-45D5-87B0-29F7290F63E1} = {F5798C6F-98D6-4411-A5F0-F928CBA64095}
155162
EndGlobalSection
156163
GlobalSection(ExtensibilityGlobals) = postSolution
157164
SolutionGuid = {961EEBAB-4149-4AA4-BEE7-9DAAE4C94133}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.AI" Version="9.3.0-preview.1.25114.11" />
12+
<PackageReference Include="Microsoft.Extensions.AI.Ollama" Version="9.3.0-preview.1.25114.11" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.Extensions.AI;
2+
using System.ComponentModel;
3+
4+
var ollamaEndpoint = "http://localhost:11434";
5+
var chatModel = "llama3.2";
6+
7+
IChatClient client = new OllamaChatClient(
8+
endpoint: ollamaEndpoint,
9+
modelId: chatModel)
10+
.AsBuilder()
11+
.UseFunctionInvocation()
12+
.Build();
13+
14+
ChatOptions options = new ChatOptions
15+
{
16+
Tools = [
17+
AIFunctionFactory.Create(GetTheWeather)
18+
]
19+
};
20+
21+
var question = "Solve 2+2. Provide an accurate and short answer";
22+
Console.WriteLine($"question: {question}");
23+
var response = await client.GetResponseAsync(question, options);
24+
Console.WriteLine($"response: {response}");
25+
26+
Console.WriteLine();
27+
28+
question = "Do I need an umbrella today?. Provide an accurate and short answer";
29+
Console.WriteLine($"question: {question}");
30+
response = await client.GetResponseAsync(question, options);
31+
Console.WriteLine($"response: {response}");
32+
33+
34+
35+
[Description("Get the weather")]
36+
static string GetTheWeather()
37+
{
38+
Console.WriteLine("\tGetTheWeather function invoked.");
39+
40+
var temperature = Random.Shared.Next(5, 20);
41+
var conditions = Random.Shared.Next(0, 1) == 0 ? "sunny" : "rainy";
42+
var weather = $"The weather is {temperature} degrees C and {conditions}.";
43+
Console.WriteLine($"\tGetTheWeather result: {weather}.");
44+
return weather;
45+
}

0 commit comments

Comments
 (0)