Skip to content

Instantly share code, notes, and snippets.

if (completion.FinishReason == ChatFinishReason.ToolCalls)
{
try
{
var toolResult = string.Empty;
foreach (var toolCall in completion.ToolCalls)
{
switch (toolCall.FunctionName)
{
case "GetImageDataAsync":
@pavlovmilen
pavlovmilen / chatclientcode.cs
Last active September 21, 2024 17:41
Chat client setup
// 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>
@pavlovmilen
pavlovmilen / CreateImageContextTool.cs
Created September 21, 2024 16:45
CreateImageContextTool
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": {
@pavlovmilen
pavlovmilen / GetImageDataAsync.cs
Created September 21, 2024 16:43
Get image binary data with content type
public async Task<ImageResponse> GetImageDataAsync(string imageLink)
{
try
{
var imageResponse = new ImageResponse();
using var response = await _httpClient.GetAsync(imageLink);
response.EnsureSuccessStatusCode();
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));
@pavlovmilen
pavlovmilen / Functions.cs
Created September 2, 2024 15:17
Functions definition
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;
@pavlovmilen
pavlovmilen / ChatToolGenerator.cd
Created September 2, 2024 15:14
ChatToolGenerator
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",
res = app.app_state.collection.add(
documents=[document],
embeddings=[app.app_state.get_text_embedding(document)],
metadatas=[metadata],
ids=[ids]
)
@pavlovmilen
pavlovmilen / app.py
Created July 14, 2024 10:31
Get metadatas
documents = app.app_state.collection.get(include=["metadatas"])
@pavlovmilen
pavlovmilen / app.py
Created July 14, 2024 10:27
Filter on metadata
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,