feat: implement tool call

This commit is contained in:
Stevan Freeborn
2024-07-01 21:00:27 -05:00
parent cccd0e6647
commit ce6b1bf8dc
5 changed files with 354 additions and 9 deletions
+30
View File
@@ -82,6 +82,12 @@ public class AnthropicApiClient : IAnthropicApiClient
}
var chatResponse = Deserialize<ChatResponse>(responseContent) ?? new ChatResponse();
if (request.Tools is not null && request.Tools.Count > 0)
{
chatResponse.ToolCall = GetToolCall(chatResponse, request.Tools);
}
return AnthropicResult<ChatResponse>.Success(chatResponse, anthropicHeaders);
}
@@ -190,6 +196,11 @@ public class AnthropicApiClient : IAnthropicApiClient
Usage = newUsage,
Content = chatResponse.Content,
};
if (request.Tools is not null && request.Tools.Count > 0)
{
chatResponse.ToolCall = GetToolCall(chatResponse, request.Tools);
}
}
// yield chat response on message stop
@@ -228,6 +239,25 @@ public class AnthropicApiClient : IAnthropicApiClient
} while (true);
}
private ToolCall? GetToolCall(ChatResponse response, List<Tool> tools)
{
var toolUse = response.Content.OfType<ToolUseContent>().FirstOrDefault();
if (toolUse is null)
{
return null;
}
var tool = tools.FirstOrDefault(t => t.Name == toolUse.Name);
if (tool is null)
{
return null;
}
return new ToolCall(tool, toolUse);
}
private async Task<HttpResponseMessage> SendRequestAsync(MessageRequest request)
{
var requestContent = new StringContent(Serialize(request), Encoding.UTF8, JsonContentType);