Skip to content

Instantly share code, notes, and snippets.

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,
@pavlovmilen
pavlovmilen / query_by_text_async.cs
Last active April 8, 2024 16:09
Query ChromaDb call in c#
public async Task<ChromaDbResult> QueryByTextAsync(string queryText)
{
try
{
var payload = new { parameters = new { query_text = queryText, n_results = 2 } };
string jsonPayload = JsonConvert.SerializeObject(payload);
var requestContent = new StringContent(jsonPayload, System.Text.Encoding.UTF8, "application/json");
var response = await _client.PostAsync("query", requestContent);
@pavlovmilen
pavlovmilen / chat_completions_api.cs
Last active April 8, 2024 14:41
Chat completion api usage
var url = _configuration.GetValue<string>("AzureOpenAiApi:Endpoint");
var key = _configuration.GetValue<string>("AzureOpenAiApi:SubscriptionKey");
var openAIClient = new OpenAIClient(new Uri(url), new AzureKeyCredential(key));
Response<ChatCompletions> responseWithoutStream =
await openAIClient.GetChatCompletionsAsync(
new ChatCompletionsOptions()
{
Messages =
{
@pavlovmilen
pavlovmilen / azure_open_ai_client_code.cs
Created April 8, 2024 13:22
Azure Open AI client code
//https://youropenaiinstance.openai.azure.com/
//c#
// Note: The Azure OpenAI client library for .NET is in preview.
// Install the .NET library via NuGet: dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.5
using Azure;
using Azure.AI.OpenAI;
OpenAIClient client = new OpenAIClient(
new Uri("https://youropenaiinstance.openai.azure.com/"),
new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY")));
@pavlovmilen
pavlovmilen / kubernetes_files.yaml
Created March 10, 2024 09:25
ChromaDb Kubernetes files
# Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: chromadb
spec:
replicas: 1
selector:
matchLabels:
app: chromadb