我在这里使用API发布了一个示例:
递归Azure OpenAI函数调用
// *** Define the Functions ***
string fnTodosPOSTDescription = "Creates a new TODO item. ";
fnTodosPOSTDescription += "Use this function to add ";
fnTodosPOSTDescription += "a new TODO item to the list";
string fnTodosGETDescription = "Retrieves the TODO list. ";
fnTodosGETDescription += "Use this function to view the TODO list.";
string fnTodosDELETEDescription = "Deletes a specific TODO item ";
fnTodosDELETEDescription += "from the list. Use this function to ";
fnTodosDELETEDescription += "remove a TODO item from the list.";
var fnTodosPOST = new FunctionDefinition();
fnTodosPOST.Name = "Todos_POST";
fnTodosPOST.Description = fnTodosPOSTDescription;
fnTodosPOST.Parameters = BinaryData.FromObjectAsJson(new JsonObject
{
["type"] = "object",
["properties"] = new JsonObject
{
["TodoRequest"] = new JsonObject
{
["type"] = "object",
["properties"] = new JsonObject
{
["todo"] = new JsonObject
{
["type"] = "string",
["description"] = @"The TODO item to be added."
}
},
["required"] = new JsonArray { "todo" }
}
},
["required"] = new JsonArray { "TodoRequest" }
});
var fnTodosGET = new FunctionDefinition();
fnTodosGET.Name = "Todos_GET";
fnTodosGET.Description = fnTodosGETDescription;
fnTodosGET.Parameters = BinaryData.FromObjectAsJson(new JsonObject
{
["type"] = "object",
["properties"] = new JsonObject { }
});
var fnTodosDELETE = new FunctionDefinition();
fnTodosDELETE.Name = "Todos_DELETE";
fnTodosDELETE.Description = fnTodosDELETEDescription;
fnTodosDELETE.Parameters = BinaryData.FromObjectAsJson(new JsonObject
{
["type"] = "object",
["properties"] = new JsonObject
{
["TodoIndexRequest"] = new JsonObject
{
["type"] = "object",
["properties"] = new JsonObject
{
["todoIdx"] = new JsonObject
{
["type"] = "integer",
["description"] = @"The index of the TODO item to be deleted."
}
},
["required"] = new JsonArray { "todoIdx" }
}
},
["required"] = new JsonArray { "TodoIndexRequest" }
});
// Create a new list of FunctionDefinition objects
List<FunctionDefinition> DefinedFunctions = new List<FunctionDefinition>();
// Add the FunctionDefinition objects to the list
DefinedFunctions.Add(fnTodosPOST);
DefinedFunctions.Add(fnTodosGET);
DefinedFunctions.Add(fnTodosDELETE);
调用Azure OpenAI服务
var chatCompletionsOptions = new ChatCompletionsOptions()
{
Temperature = (float)0.7,
MaxTokens = 2000,
NucleusSamplingFactor = (float)0.95,
FrequencyPenalty = 0,
PresencePenalty = 0,
};
chatCompletionsOptions.Functions = DefinedFunctions;
chatCompletionsOptions.FunctionCall = FunctionDefinition.Auto;
foreach (var message in ChatMessages)
{
chatCompletionsOptions.Messages.Add(message);
}
Response<ChatCompletions> responseWithoutStream =
await client.GetChatCompletionsAsync(
DeploymentOrModelName,
chatCompletionsOptions);
ChatCompletions result = responseWithoutStream.Value;
var choice = result.Choices.FirstOrDefault();
if (choice != null)
{
if (choice.Message != null)
{
ChatMessages.Add(choice.Message);
}
}
TotalTokens = TotalTokens + result.Usage.TotalTokens;
看看ChatGPT是否作为响应想要调用一个函数。
if (result.Choices.FirstOrDefault().FinishReason == "function_call")
{
bool FunctionCallingComplete = false;
while (!FunctionCallingComplete)
{
ChatMessages = ExecuteFunction(result, ChatMessages);
chatCompletionsOptions = new ChatCompletionsOptions()
{
Temperature = (float)0.7,
MaxTokens = 2000,
NucleusSamplingFactor = (float)0.95,
FrequencyPenalty = 0,
PresencePenalty = 0,
};
chatCompletionsOptions.Functions = DefinedFunctions;
chatCompletionsOptions.FunctionCall = FunctionDefinition.Auto;
foreach (var message in ChatMessages)
{
chatCompletionsOptions.Messages.Add(message);
}
Response<ChatCompletions> responseWithoutStreamFn =
await client.GetChatCompletionsAsync(
DeploymentOrModelName,
chatCompletionsOptions);
result = responseWithoutStreamFn.Value;
var FunctionResult = result.Choices.FirstOrDefault();
if (FunctionResult.Message != null)
{
ChatMessages.Add(FunctionResult.Message);
}
if (FunctionResult.FinishReason == "function_call")
{
FunctionCallingComplete = false;
}
else
{
FunctionCallingComplete = true;
}
}
}
}
catch (Exception ex)
{
ErrorMessage = ex.Message;
}
finally
{
prompt = "";
Processing = false;
StateHasChanged();
}
执行一个函数
private List<ChatMessage> ExecuteFunction(
ChatCompletions ChatCompletionResult, List<ChatMessage> ParamChatPrompts)
{
// Get the arguments
var functionArgs =
ChatCompletionResult.Choices.FirstOrDefault()
.Message.FunctionCall.Arguments.ToString();
// Get the function name
var functionName =
ChatCompletionResult.Choices.FirstOrDefault()
.Message.FunctionCall.Name;
// Variable to hold the function result
string functionResult = "";
// Use select case to call the function
switch (functionName)
{
case "Todos_POST":
var NewTODO =
JsonSerializer.Deserialize<ToDoAddRequest>(functionArgs);
if (NewTODO != null)
{
functionResult = AddTodo(NewTODO.TodoRequest.todo);
}
break;
case "Todos_GET":
functionResult = GetTodos();
break;
case "Todos_DELETE":
var DeleteTODO =
JsonSerializer.Deserialize<ToDoRemoveRequest>(functionArgs);
if (DeleteTODO != null)
{
functionResult =
DeleteTodo(DeleteTODO.TodoIndexRequest.todoIdx);
}
break;
default:
break;
}
// Return with the results of the function
var ChatFunctionMessage = new ChatMessage();
ChatFunctionMessage.Role = ChatRole.Function;
ChatFunctionMessage.Content = functionResult;
ChatFunctionMessage.Name = functionName;
ParamChatPrompts.Add(ChatFunctionMessage);
return ParamChatPrompts;
}