This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (completion.FinishReason == ChatFinishReason.ToolCalls) | |
{ | |
try | |
{ | |
var toolResult = string.Empty; | |
foreach (var toolCall in completion.ToolCalls) | |
{ | |
switch (toolCall.FunctionName) | |
{ | |
case "GetImageDataAsync": |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// chat client setup code | |
var url = _configuration.GetValue<string>("AzureOpenAiApi:Endpoint"); | |
var key = _configuration.GetValue<string>("AzureOpenAiApi:SubscriptionKey"); | |
var openAIClient = new AzureOpenAIClient(new Uri(url), new AzureKeyCredential(key)); | |
var chatClient = openAIClient.GetChatClient(_configuration.GetValue<string>("AzureOpenAiApi:DeploymentName")); | |
// add our chat tool so its available if needed | |
chatCompletionOptions.Tools.Add(ChatToolService.CreateImageContextTool()); | |
var inputMessages = new List<ChatMessage> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static ChatTool CreateImageContextTool() | |
{ | |
var imageContextTool = ChatTool.CreateFunctionTool( | |
functionName: "GetImageDataAsync", | |
functionDescription: "Get image bytes from a given image link", | |
functionParameters: BinaryData.FromString(""" | |
{ | |
"type": "object", | |
"properties": { | |
"imageLink": { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<ImageResponse> GetImageDataAsync(string imageLink) | |
{ | |
try | |
{ | |
var imageResponse = new ImageResponse(); | |
using var response = await _httpClient.GetAsync(imageLink); | |
response.EnsureSuccessStatusCode(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<ApiResponse<string>> ChatWithChatGPTAsync(string context, string myQuestion) | |
{ | |
var result = new ApiResponse<string>(); | |
try | |
{ | |
var url = _configuration.GetValue<string>("AzureOpenAiApi:Endpoint"); | |
var key = _configuration.GetValue<string>("AzureOpenAiApi:SubscriptionKey"); | |
var openAIClient = new AzureOpenAIClient(new Uri(url), new AzureKeyCredential(key)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using LCI.Services.Abstract; | |
using LCI.Services.Core.DtoModels; | |
using LCI.Services.Core.Services; | |
using Microsoft.Extensions.Logging; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ChatToolGenerator | |
{ | |
public static ChatTool CreateCrimeContextTool() | |
{ | |
var crimeContextTool = ChatTool.CreateFunctionTool( | |
functionName: "CrimeAtLocationForPostcode", | |
functionDescription: "Produce crime context for a given postcode", | |
functionParameters: BinaryData.FromString(""" | |
{ | |
"type": "object", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
res = app.app_state.collection.add( | |
documents=[document], | |
embeddings=[app.app_state.get_text_embedding(document)], | |
metadatas=[metadata], | |
ids=[ids] | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
documents = app.app_state.collection.get(include=["metadatas"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if metadata: | |
res = app.app_state.collection.query( | |
query_embeddings=query_embeddings, | |
n_results=results, | |
where={"Metadata": metadata}, | |
include=["documents", "metadatas", "distances", "embeddings"] | |
) | |
else: | |
res = app.app_state.collection.query( | |
query_embeddings=query_embeddings, |
NewerOlder