Skip to content

elbruno/ws-add-ai-to-net-apps

Repository files navigation

Adding AI to Your Existing .NET Apps - Workshop

License: MIT Twitter: elbruno GitHub: elbruno

Welcome to the Adding AI to Your Existing .NET Apps Workshop. This repository contains a set of .NET projects to demostrate how to add AI to a common .NET scenario: Winform as front end app, and an WebAPI as backend.

The following image shows the WinForm Chat App interacting with an GPT-4o model in Azure OpenAI, and metrics traced using Aspire Dashboard.

These are the different steps included in the workshop:

  1. Standard Winform + API NET Chat App
  2. Add Azure OpenAI GPT-4o to support chat (text)
  3. Add Azure OpenAI GPT-4o to support chat + image (text)
  4. Add Phi-3 to support chat using ONNX (text)
  5. Add Phi-3 to support chat + image using ONNX (text)
  6. Add Phi-3 to support chat using Ollama in docker(text)
  7. Add Cache using Semantic Memory, local in Memory
  8. Add Cache using Semantic Memory, store in Azure AI Search
  9. Add RAG using Azure AI Search
  10. Add Aspire Dashboard using OTLP endpoint
  11. Use an external API using native functions

Prerequisites

Before running the sample, ensure you have the following installed:

  • .NET 8: Make sure you have the latest version of .NET installed on your machine.

  • Visual Studio or Visual Studio Code: You will need an IDE or code editor capable of running .NET projects. Visual Studio or Visual Studio Code are recommended.

  • Azure OpenAI Services: To GPT-4o as the model to support the chat, you need to have access to Azure OpenAI Services and create and deploy a GPT-4o model.

  • Ollama: To use Phi-3 as local model with Ollama, you need to have ollama installed.

  • Docker Desktop: To use Phi-3 as local model with Ollama, you need to have docker installed.

  • Super Hero API: To use the native functions demo, get a free key for the SuperHero API.

Project Secrets

The chat-server project uses .NET User Secrets to access remote resources like Azure OpenAI Services or Azure AI Search.

Follow these steps to add the secrets to the project.

  1. Open a terminal and navigate to the chat-server project.

    cd .\src\chat-server\
  2. Run the following command with the correct values

    dotnet user-secrets init
    dotnet user-secrets set "AZURE_OPENAI_MODEL" "< Azure OpenAI Model, ie: gpt-4o >"
    dotnet user-secrets set "AZURE_OPENAI_ENDPOINT" "< Azure OpenAI Endpoint >"
    dotnet user-secrets set "AZURE_OPENAI_APIKEY" "< Azure OpenAI Key >"
    dotnet user-secrets set "AZURE_OPENAI_ADA02" "< Azure OpenAI Embeddings model, ie: text-embedding-ada-002 >"
    dotnet user-secrets set "AZURE_AISEARCH_ENDPOINT" "< Azure AI Search Endpoint >"
    dotnet user-secrets set "AZURE_AISEARCH_APIKEY" "< Azure AI Search ApiKey >"
    dotnet user-secrets set "AZURE_AISEARCH_INDEXNAME" "< Azure AI Search IndexName for RAG >"
    dotnet user-secrets set "OTLP_ENDPOINT" "< OTLP ENDPOINT, ie: http://localhost:4317 >"
    dotnet user-secrets set "SUPERHERO_APIKEY" "< super hero api key >"

Install Phi-3 locally, to be used in the ONNX scenarios

  • Create a local folder to store the Phi-3 local model. In example: C:\phi3\models.

  • Download the phi3-mini-4k-instruct-onnx model to your local machine:

    # navigate to the folder to store the models
    cd c:\phi3\models
    
    # add support for lfs
    git lfs install 
    
    # clone and download mini 4K instruct model
    git clone https://huggingface.co/microsoft/Phi-3-mini-4k-instruct-onnx
    
    # clone and download vision 128K model
    git clone https://huggingface.co/microsoft/Phi-3-vision-128k-instruct-onnx-cpu

    Important: The current demos are designed to use the ONNX versions of the model. The previous steps clone the following models.

Docker Support Commands

Run this command to get the Aspire dashboard up and running in docker:

docker run --rm -it -p 18888:18888 -p 4317:18889 -d --name aspire-dashboard mcr.microsoft.com/dotnet/aspire-dashboard:8.0.0

Run this command to host ollama in Docker:

docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama

Live Demo Recording

Coming soon ...

Resources

Author

👤 Bruno Capuano

🤝 Contributing

Contributions, issues and feature requests are welcome!

Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2024 Bruno Capuano.

This project is MIT licensed.


About

Adding AI to Your Existing .NET Apps - Workshop

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages