Last active
October 23, 2025 17:17
-
-
Save medhatelmasry/9e9e0cece9863b1bd1066b38be04dd45 to your computer and use it in GitHub Desktop.
AI Toolkit OpenAI Client - Program.cs
This file contains hidden or 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 OpenAI; | |
| using OpenAI.Chat; | |
| using System.ClientModel; | |
| using System.Text; | |
| var model = "mistral-7b-v02-int4-cpu"; | |
| var baseUrl = "http://localhost:5272/v1/"; // root URL for local OpenAI-like server | |
| var apikey = "unused"; | |
| OpenAIClientOptions options = new OpenAIClientOptions(); | |
| options.Endpoint = new Uri(baseUrl); | |
| ApiKeyCredential credential = new ApiKeyCredential(apikey); | |
| ChatClient client = new OpenAIClient(credential, options).GetChatClient(model); | |
| // Build the prompt | |
| StringBuilder prompt = new StringBuilder(); | |
| prompt.AppendLine("You will analyze the sentiment of the following product reviews."); | |
| prompt.AppendLine("Each line is its own review. Output the sentiment of each review in"); | |
| prompt.AppendLine("a bulleted list and then provide a general sentiment of all reviews."); | |
| prompt.AppendLine(); | |
| prompt.AppendLine("I bought this product and it's amazing. I love it!"); | |
| prompt.AppendLine("This product is terrible. I hate it."); | |
| prompt.AppendLine("I'm not sure about this product. It's okay."); | |
| prompt.AppendLine("I found this product based on the other reviews. It worked"); | |
| // send the prompt to the model and wait for the text completion | |
| var response = await client.CompleteChatAsync(prompt.ToString()); | |
| // display the response | |
| Console.WriteLine(response.Value.Content[0].Text); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment