forked from SciSharp/LLamaSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SemanticKernelFunctionCalling.cs
138 lines (113 loc) · 4.5 KB
/
SemanticKernelFunctionCalling.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using LLama;
using LLama.Common;
using LLamaSharp.SemanticKernel.ChatCompletion;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Newtonsoft.Json.Linq;
using ChatHistory = Microsoft.SemanticKernel.ChatCompletion.ChatHistory;
namespace LLama.Examples.Examples;
public class SemanticKernelFunctionCalling
{
public static async Task Run()
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Use this model: " +
"https://huggingface.co/Trelis/Llama-2-7b-chat-hf-function-calling-v3");
var modelPath = UserSettings.GetModelPath();
var parameters = new ModelParams(modelPath);
var weights = LLamaWeights.LoadFromFile(parameters);
var executor = new StatelessExecutor(weights, parameters);
// Create kernel
var builder = Kernel.CreateBuilder();
builder.Services.AddSingleton<IChatCompletionService>(new LLamaSharpChatCompletion(executor));
builder.Services.AddLogging(c => c.AddDebug().SetMinimumLevel(LogLevel.Trace));
var plugin = builder.Plugins.AddFromType<MathPlugin>();
var kernel = builder.Build();
// Create chat history
ChatHistory history = [];
var plugins = kernel.Plugins.GetFunctionsMetadata();
var functionsPrompt = CreateFunctionsMetaObject(plugins);
// Prompt for llama hf function calling v3
var prompt = $"""
[INST] You have access to the following functions. Use them if required:
{functionsPrompt}
""";
history.AddSystemMessage(prompt);
// Get chat completion service
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
// Start the conversation
while (true)
{
// Get user input
Console.Write("User > ");
var userMessage = Console.ReadLine()!;
history.AddUserMessage(userMessage);
ChatRequestSettings llamaSettings = new()
{
AutoInvoke = true,
};
var result = chatCompletionService.GetStreamingChatMessageContentsAsync(
history,
executionSettings: llamaSettings,
kernel: kernel);
Console.Write("Assistant > ");
var lastMessage = "";
await foreach (var content in result)
{
Console.Write(content.ToString());
lastMessage += content.ToString();
}
// var lastMessage = result.Last().Content;
// Console.WriteLine(lastMessage);
Console.WriteLine();
// Add the message from the agent to the chat history
history.AddAssistantMessage(lastMessage);
}
}
private static JToken? CreateFunctionsMetaObject(IList<KernelFunctionMetadata> plugins)
{
if (plugins.Count < 1) return null;
if (plugins.Count == 1) return CreateFunctionMetaObject(plugins[0]);
JArray promptFunctions = [];
foreach (var plugin in plugins)
{
var pluginFunctionWrapper = CreateFunctionMetaObject(plugin);
promptFunctions.Add(pluginFunctionWrapper);
}
return promptFunctions;
}
private static JObject CreateFunctionMetaObject(KernelFunctionMetadata plugin)
{
var pluginFunctionWrapper = new JObject()
{
{ "type", "function" },
};
var pluginFunction = new JObject()
{
{ "name", plugin.Name },
{ "description", plugin.Description },
};
var pluginFunctionParameters = new JObject()
{
{ "type", "object" },
};
var pluginProperties = new JObject();
foreach (var parameter in plugin.Parameters)
{
var property = new JObject()
{
{ "type", parameter.ParameterType?.ToString() },
{ "description", parameter.Description },
};
pluginProperties.Add(parameter.Name, property);
}
pluginFunctionParameters.Add("properties", pluginProperties);
pluginFunction.Add("parameters", pluginFunctionParameters);
pluginFunctionWrapper.Add("function", pluginFunction);
return pluginFunctionWrapper;
}
}