Код: Выделить всё
var createAppointmenFunction = new FunctionDefinitionBuilder("CreateAppointment", "Create an appointment at the specified time")
.AddParameter("appointment", PropertyDefinition.DefineString("short description"))
.AddParameter("date", PropertyDefinition.DefineString("date of the appointment"))
.Validate()
.Build();
var tools = new List
{
new() { Function = createAppointmenFunction, Type = "function" },
};
var messages = new List
{
ChatMessage.FromSystem("You're personal assistance"),
ChatMessage.FromUser(Console.ReadLine()!)
};
var request = new ChatCompletionCreateRequest
{
Messages = messages,
Tools = tools,
MaxTokens = 300,
N = 1,
Model = Models.Gpt_3_5_Turbo_1106
};
var service = new OpenAIService(
new OpenAiOptions
{
ApiKey = "key"
});
while (true)
{
var result = service.ChatCompletion.CreateCompletion(request).GetAwaiter().GetResult();
if (result.Successful)
{
var toolCalls = result.Choices.First().Message.ToolCalls;
if (toolCalls != null)
{
var funcCall = toolCalls.First().FunctionCall!;
var funcArgs = funcCall.ParseArguments();
if (funcCall.Name == "CreateAppointment")
{
// it's called
CreateAppointment(Convert.ToString(funcArgs["appointment"])!, DateTime.Now); // just prints into console
}
// save input from a "custom tool"
messages.Add(ChatMessage.FromTool("Your visit is scheduled for tomorrow", toolCalls.First().Id));
}
else
{
Console.WriteLine("==> " + string.Join("\n", result.Choices.Select(i => $"\t\t{i.Message.Content}")));
messages.Add(ChatMessage.FromAssistant(result.Choices.Single().Message.Content));
// new input from user
messages.Add(ChatMessage.FromUser(Console.ReadLine()!));
}
}
else
{
Console.WriteLine(result.Error.Message);
}
}
Код: Выделить всё
result.SuccessfulНедопустимый параметр: сообщения с ролью «инструмент» должны быть ответом на предыдущее сообщение с «tool_calls»
Очевидно, ChatMessage.FromTool — неправильный путь, но когда я использую ChatMessage.FromAssistant, он тоже терпит неудачу. Настроить ролевую функцию напрямую (так как нет подходящего статического метода) также не удается.
Подробнее здесь: https://stackoverflow.com/questions/786 ... pen-ai-llm
Мобильная версия