Skip to content

Commit 351207d

Browse files
committed
cohere v2 endpoints wip
1 parent b0b2ffc commit 351207d

File tree

11 files changed

+397
-363
lines changed

11 files changed

+397
-363
lines changed

src/LlmTornado.Demo/ChatDemo.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,32 @@ public static async Task<bool> ChatFunctionRequired()
144144
return false;
145145
}
146146

147+
[TornadoTest]
148+
public static async Task<bool> CohereTool()
149+
{
150+
Conversation chat = Program.Connect().Chat.CreateConversation(new ChatRequest
151+
{
152+
Model = ChatModel.Cohere.Command.A0325,
153+
Tools = [new Tool(new ToolFunction("get_weather", "gets the current weather"), true)],
154+
ToolChoice = new OutboundToolChoice(OutboundToolChoiceModes.Required),
155+
Messages = [
156+
new ChatMessage(ChatMessageRoles.User, "What is the weather like today?")
157+
]
158+
});
159+
160+
ChatRichResponse response = await chat.GetResponseRich();
161+
162+
ChatRichResponseBlock? block = response.Blocks?.FirstOrDefault(x => x.Type is ChatRichResponseBlockTypes.Function);
163+
164+
if (block is not null)
165+
{
166+
Console.WriteLine($"fn block found: {block.FunctionCall?.Name}");
167+
return true;
168+
}
169+
170+
return false;
171+
}
172+
147173
[TornadoTest]
148174
public static async Task<bool> ChatFunctionGemini()
149175
{

src/LlmTornado/Chat/ChatRequest.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ private static string PreparePayload(JObject sourceObject, ChatRequest context,
504504
return sourceObject.ToString(settings?.Formatting ?? Formatting.None);
505505
}
506506

507-
private static readonly Dictionary<LLmProviders, Func<ChatRequest, IEndpointProvider, CapabilityEndpoints, JsonSerializerSettings?, string>> SerializeMap = new Dictionary<LLmProviders, Func<ChatRequest, IEndpointProvider, CapabilityEndpoints, JsonSerializerSettings?, string>>((int)LLmProviders.Length)
507+
private static readonly Dictionary<LLmProviders, Func<ChatRequest, IEndpointProvider, CapabilityEndpoints, JsonSerializerSettings?, string>> serializeMap = new Dictionary<LLmProviders, Func<ChatRequest, IEndpointProvider, CapabilityEndpoints, JsonSerializerSettings?, string>>((int)LLmProviders.Length)
508508
{
509509
{
510510
LLmProviders.OpenAi, (x, y, z, a) =>
@@ -530,7 +530,14 @@ private static string PreparePayload(JObject sourceObject, ChatRequest context,
530530
},
531531
{ LLmProviders.DeepSeek, (x, y, z, a) => PreparePayload(x, x, y, z, GetSerializer(EndpointBase.NullSettings, a)) },
532532
{ LLmProviders.Anthropic, (x, y, z, a) => PreparePayload(new VendorAnthropicChatRequest(x, y), x, y, z, GetSerializer(EndpointBase.NullSettings, a)) },
533-
{ LLmProviders.Cohere, (x, y, z, a) => PreparePayload(new VendorCohereChatRequest(x, y), x, y, z, GetSerializer(EndpointBase.NullSettings, a)) },
533+
{
534+
LLmProviders.Cohere, (x, y, z, a) =>
535+
{
536+
VendorCohereChatRequest request = new VendorCohereChatRequest(x, y);
537+
JsonSerializerSettings serializer = GetSerializer(EndpointBase.NullSettings, a);
538+
return PreparePayload(request.Serialize(serializer), x, y, z, serializer);
539+
}
540+
},
534541
{ LLmProviders.Google, (x, y, z, a) => PreparePayload(new VendorGoogleChatRequest(x, y), x, y, z, GetSerializer(EndpointBase.NullSettings, a)) },
535542
{
536543
LLmProviders.Mistral, (x, y, z, a) =>
@@ -730,7 +737,7 @@ private TornadoRequestContent Serialize(IEndpointProvider provider, CapabilityEn
730737
outboundCopy.ReasoningFormat = null;
731738
}
732739

733-
TornadoRequestContent serialized = SerializeMap.TryGetValue(provider.Provider, out Func<ChatRequest, IEndpointProvider, CapabilityEndpoints, JsonSerializerSettings?, string>? serializerFn) ? new TornadoRequestContent(serializerFn.Invoke(outboundCopy, provider, capabilityEndpoint, pretty ? new JsonSerializerSettings
740+
TornadoRequestContent serialized = serializeMap.TryGetValue(provider.Provider, out Func<ChatRequest, IEndpointProvider, CapabilityEndpoints, JsonSerializerSettings?, string>? serializerFn) ? new TornadoRequestContent(serializerFn.Invoke(outboundCopy, provider, capabilityEndpoint, pretty ? new JsonSerializerSettings
734741
{
735742
Formatting = Formatting.Indented
736743
} : null), outboundCopy.Model, outboundCopy.UrlOverride, provider, capabilityEndpoint) : new TornadoRequestContent(string.Empty, outboundCopy.Model, outboundCopy.UrlOverride, provider, CapabilityEndpoints.Chat);

src/LlmTornado/Chat/ChatResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ internal ChatUsage(VendorPerplexityUsage usage)
295295

296296
internal ChatUsage(VendorCohereUsage usage)
297297
{
298-
CompletionTokens = usage.BilledUnits.OutputTokens;
299-
PromptTokens = usage.BilledUnits.OutputTokens;
298+
CompletionTokens = usage.BilledUnits?.OutputTokens ?? 0;
299+
PromptTokens = usage.BilledUnits?.OutputTokens ?? 0;
300300
TotalTokens = CompletionTokens + PromptTokens;
301301
VendorUsageObject = usage;
302302
Provider = LLmProviders.Cohere;

src/LlmTornado/Chat/Vendors/Cohere/ChatResponseVendorCohereExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public ChatResponseVendorCohereExtensions()
4949
/// <returns></returns>
5050
public List<VendorCohereCitationBlock> ParseCitations()
5151
{
52-
return ParseCitations(Response.Text ?? string.Empty);
52+
return []; // ParseCitations(Response.Message ?? string.Empty);
5353
}
5454

5555
/// <summary>
@@ -128,10 +128,10 @@ string ClearSnippet(string txt)
128128

129129
internal ChatResponseVendorCohereExtensions(VendorCohereChatResult response)
130130
{
131-
Citations = response.Citations;
131+
/*Citations = response.Citations;
132132
Documents = response.Documents;
133133
SearchQueries = response.SearchQueries;
134134
SearchResults = response.SearchResults;
135-
Response = response;
135+
Response = response;*/
136136
}
137137
}

0 commit comments

Comments
 (0)