Skip to content

Commit 501fa81

Browse files
committed
Add BasicChat-06OpenAIAPIs project for sentiment analysis
Introduced a new project, `BasicChat-06OpenAIAPIs`, to the solution, targeting .NET 9.0 and integrating OpenAI's GPT model for sentiment analysis. Updated the solution file to support Visual Studio 18 and added build configurations for the new project. The project includes: - A `Program.cs` file demonstrating API key retrieval, prompt creation, and interaction with OpenAI's `ChatClient`. - Dependencies for AI and configuration management, including `Microsoft.Extensions.AI` and `OpenAI`. These changes enhance the solution with generative AI capabilities.
1 parent 5a69ae5 commit 501fa81

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<RootNamespace>BasicChat_06OpenAIAPIs</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
<UserSecretsId>82dba96a-152d-496e-8762-c2e93f97059c</UserSecretsId>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.Extensions.AI" Version="9.7.1" />
14+
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.7.1-preview.1.25365.4" />
15+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.7" />
16+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.7" />
17+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.7" />
18+
<PackageReference Include="OpenAI" Version="2.2.0" />
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.Extensions.AI;
2+
using Microsoft.Extensions.Configuration;
3+
using System.Text;
4+
5+
var openai_apikey = Environment.GetEnvironmentVariable("APIKEY");
6+
if (string.IsNullOrEmpty(openai_apikey))
7+
{
8+
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
9+
openai_apikey = config["APIKEY"];
10+
}
11+
12+
IChatClient client =
13+
new OpenAI.Chat.ChatClient("gpt-4.1-mini", openai_apikey)
14+
.AsIChatClient();
15+
16+
// here we're building the prompt
17+
StringBuilder prompt = new StringBuilder();
18+
prompt.AppendLine("You will analyze the sentiment of the following product reviews. Each line is its own review. Output the sentiment of each review in a bulleted list and then provide a generate sentiment of all reviews. ");
19+
prompt.AppendLine("I bought this product and it's amazing. I love it!");
20+
prompt.AppendLine("This product is terrible. I hate it.");
21+
prompt.AppendLine("I'm not sure about this product. It's okay.");
22+
prompt.AppendLine("I found this product based on the other reviews. It worked for a bit, and then it didn't.");
23+
24+
// send the prompt to the model and wait for the text completion
25+
var response = await client.GetResponseAsync(prompt.ToString());
26+
27+
// display the response
28+
Console.WriteLine(response.Text);

03-CoreGenerativeAITechniques/src/CoreGenerativeAITechniques.sln

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 17
4-
VisualStudioVersion = 17.12.35514.174
3+
# Visual Studio Version 18
4+
VisualStudioVersion = 18.0.10829.221 main
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicChat-01MEAI", "BasicChat-01MEAI\BasicChat-01MEAI.csproj", "{F924B9FC-D806-4350-BEC8-C0D4BB9E7C6D}"
77
EndProject
@@ -105,6 +105,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MCP-01-HuggingFace", "MCP-0
105105
EndProject
106106
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MCP-02-HuggingFace-Ollama", "MCP-02-HuggingFace-Ollama\MCP-02-HuggingFace-Ollama.csproj", "{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}"
107107
EndProject
108+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicChat-06OpenAIAPIs", "BasicChat-06OpenAIAPIs\BasicChat-06OpenAIAPIs.csproj", "{48371925-76C4-40BD-A6F4-95E983771191}"
109+
EndProject
108110
Global
109111
GlobalSection(SolutionConfigurationPlatforms) = preSolution
110112
Debug|Any CPU = Debug|Any CPU
@@ -559,6 +561,18 @@ Global
559561
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Release|x64.Build.0 = Release|Any CPU
560562
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Release|x86.ActiveCfg = Release|Any CPU
561563
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27}.Release|x86.Build.0 = Release|Any CPU
564+
{48371925-76C4-40BD-A6F4-95E983771191}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
565+
{48371925-76C4-40BD-A6F4-95E983771191}.Debug|Any CPU.Build.0 = Debug|Any CPU
566+
{48371925-76C4-40BD-A6F4-95E983771191}.Debug|x64.ActiveCfg = Debug|Any CPU
567+
{48371925-76C4-40BD-A6F4-95E983771191}.Debug|x64.Build.0 = Debug|Any CPU
568+
{48371925-76C4-40BD-A6F4-95E983771191}.Debug|x86.ActiveCfg = Debug|Any CPU
569+
{48371925-76C4-40BD-A6F4-95E983771191}.Debug|x86.Build.0 = Debug|Any CPU
570+
{48371925-76C4-40BD-A6F4-95E983771191}.Release|Any CPU.ActiveCfg = Release|Any CPU
571+
{48371925-76C4-40BD-A6F4-95E983771191}.Release|Any CPU.Build.0 = Release|Any CPU
572+
{48371925-76C4-40BD-A6F4-95E983771191}.Release|x64.ActiveCfg = Release|Any CPU
573+
{48371925-76C4-40BD-A6F4-95E983771191}.Release|x64.Build.0 = Release|Any CPU
574+
{48371925-76C4-40BD-A6F4-95E983771191}.Release|x86.ActiveCfg = Release|Any CPU
575+
{48371925-76C4-40BD-A6F4-95E983771191}.Release|x86.Build.0 = Release|Any CPU
562576
EndGlobalSection
563577
GlobalSection(SolutionProperties) = preSolution
564578
HideSolutionNode = FALSE
@@ -601,6 +615,7 @@ Global
601615
{07F43AC9-EBB1-4F21-BC51-6DAD0F2F0E55} = {1248F5FA-C7D6-4876-8313-A15A942F84FE}
602616
{769320A2-CE09-41EF-A345-8BE6ABBE2696} = {3D13EC01-716A-41D9-BC54-1871EC14D3DE}
603617
{CB155DBA-0E2D-437D-AB29-FF5B65B44A27} = {3D13EC01-716A-41D9-BC54-1871EC14D3DE}
618+
{48371925-76C4-40BD-A6F4-95E983771191} = {1248F5FA-C7D6-4876-8313-A15A942F84FE}
604619
EndGlobalSection
605620
GlobalSection(ExtensibilityGlobals) = postSolution
606621
SolutionGuid = {961EEBAB-4149-4AA4-BEE7-9DAAE4C94133}

0 commit comments

Comments
 (0)