Skip to content

Commit

Permalink
new samples based on the OpenAI Hike demos
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenkirstaetter committed Apr 26, 2024
1 parent 7b4326c commit 96a4602
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>bd5c311f-6c35-4d21-badd-1b9d9b76b66b</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Mscc.GenerativeAI" Version="1.1.4" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions samples/Console.HikeBenefitsSummary/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.Extensions.Configuration;
using Mscc.GenerativeAI;

// Read the API key from the user secrets file.
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string apiKey = config["GOOGLE_API_KEY"];

// Create a new Google AI model client.
var genai = new GoogleAI(apiKey);
var model = genai.GenerativeModel(model: Model.Gemini15Pro);

// Read the text to be summarized from a Markdown file.
var markdown = System.IO.File.ReadAllText("benefits.md");

// Instruct the model to summarize the text in 20 words or less.
var prompt = $"Please summarize the the following text in 20 words or less: {markdown}";
Console.WriteLine($"\nUser >>> {prompt}");

// Generate the summary and display it.
var response = await model.GenerateContent(prompt);
Console.WriteLine($"\nGemini >>> {response.Text}");
3 changes: 3 additions & 0 deletions samples/Console.HikeBenefitsSummary/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hiking Benefits Summary

This sample demonstrates how to use Gemini AI with a `gemini-1.5-pro` model, from a .NET 8.0 console application. Use the AI model to summarize a page of text to a few words. It consists of a console application, running locally, that will read the file `benefits.md` and send request to the Gemini API to summarize it.
21 changes: 21 additions & 0 deletions samples/Console.HikeBenefitsSummary/benefits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Hiking Benefits

**Hiking** is a wonderful activity that offers a plethora of benefits for both your body and mind. Here are some compelling reasons why you should consider starting hiking:
1. **Physical Fitness**:
- **Cardiovascular Health**: Hiking gets your heart pumping, improving cardiovascular fitness. The varied terrain challenges your body and burns calories.
- Strength and Endurance: Uphill climbs and uneven trails engage different muscle groups, enhancing strength and endurance.
- Weight Management: Regular hiking can help you maintain a healthy weight.
2. Mental Well-Being:
- Stress Reduction: Nature has a calming effect. Hiking outdoors reduces stress, anxiety, and promotes relaxation.
- Improved Mood: Fresh air, sunlight, and natural surroundings boost your mood and overall happiness.
- Mindfulness: Disconnect from screens and immerse yourself in the present moment. Hiking encourages mindfulness.
3. Connection with Nature:
- Scenic Views: Explore breathtaking landscapes, from lush forests to mountain peaks. Nature's beauty rejuvenates the soul.
- Wildlife Encounters: Spot birds, animals, and plant life. Connecting with nature fosters appreciation and wonder.
4. Social Interaction:
- Group Hikes: Join hiking clubs or go with friends. It's a great way to bond and share experiences.
- Solitude: Solo hikes provide introspection and solitude, allowing you to recharge.
5. Adventure and Exploration:
- Discover Hidden Gems: Hiking takes you off the beaten path. Discover hidden waterfalls, caves, and scenic trails.
- Sense of Accomplishment: Reaching a summit or completing a challenging trail gives a sense of achievement.
Remember, hiking can be tailored to your fitness level—start with shorter, easier trails and gradually progress. Lace up those hiking boots and embark on an adventure! 🌲🥾
16 changes: 16 additions & 0 deletions samples/Console.HikeChatter/Console.HikeChatter.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>bd5c311f-6c35-4d21-badd-1b9d9b76b66b</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Mscc.GenerativeAI" Version="1.1.4" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions samples/Console.HikeChatter/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.Extensions.Configuration;
using Mscc.GenerativeAI;

// Read the API key from the user secrets file.
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string apiKey = config["GOOGLE_API_KEY"];

// == Providing context for the AI model ==========
var markdown = System.IO.File.ReadAllText("hikes.md");
var systemPrompt = new Content { Parts = new() { new TextData() { Text =
"""
You are upbeat and friendly. You introduce yourself when first saying hello.
Provide a short answer only based on the user hiking records below:
""" + markdown
}}};
Console.WriteLine($"\n\t-=-=- Hiking History -=-=--\n{markdown}");

// Create a new Google AI model client.
var genai = new GoogleAI(apiKey);
var generationConfig = new GenerationConfig { Temperature = 1f, TopP = 0.95f, MaxOutputTokens = 1000 };
var model = genai.GenerativeModel(model: Model.Gemini15Pro, generationConfig: generationConfig, systemInstruction: systemPrompt);

// == Starting the conversation ==========
string userGreeting = "Hi!";
Console.WriteLine($"\nUser >>> {userGreeting}");

var response = await model.GenerateContent(userGreeting);
Console.WriteLine($"\nGemini >>> {response.Text}");

// == Providing the user's request ==========
var hikeRequest = """
I would like to know the ration of hike I did in Canada compare to hikes done in other countries.
""";
Console.WriteLine($"\nUser >>> {hikeRequest}");

// == Getting the response from the AI model ==========
response = await model.GenerateContent(hikeRequest);
Console.WriteLine($"\nGemini >>> {response.Text}");
3 changes: 3 additions & 0 deletions samples/Console.HikeChatter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Chatting About My Previous Hikes

This sample demonstrates how to use Gemini AI with a `gemini-1.5-pro` model, from a .NET 8.0 console application. Use the AI model to get analytics and information about your previous hikes. It consists of a console application, running locally, that will read the file `hikes.md` and send request to the Gemini API and provide the result in the console.
10 changes: 10 additions & 0 deletions samples/Console.HikeChatter/hikes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
| Trail Name | Hike Date | Country | Weather
| --------------- | ---------- | -------- | --------
| Cascade Falls | 2021-07-15 | Canada | Sunny
| Johnston Canyon | 2022-05-10 | Canada | Cloudy
| Lake Louise | 2020-09-05 | Canada | Rainy
| Angel's Landing | 2023-06-20 | USA | Sunny
| Gros Morne | 2021-08-25 | Canada | Foggy
| Hocking Hills | 2022-04-01 | USA | Sunny
| The Chief | 2020-07-05 | Canada | Sunny
| Skaftafell | 2022-09-10 | Iceland | Cloudy
16 changes: 16 additions & 0 deletions samples/Console.HikerAI/Console.HikerAI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>bd5c311f-6c35-4d21-badd-1b9d9b76b66b</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageReference Include="Mscc.GenerativeAI" Version="1.1.4" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions samples/Console.HikerAI/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.Extensions.Configuration;
using Mscc.GenerativeAI;

// Read the API key from the user secrets file.
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string apiKey = config["GOOGLE_API_KEY"];

// == Providing context for the AI model ==========
var systemPrompt = new Content { Parts = new() { new TextData() { Text =
"""
You are a hiking enthusiast who helps people discover fun hikes in their area. You are upbeat and friendly.
You introduce yourself when first saying hello. When helping people out, you always ask them
for this information to inform the hiking recommendation you provide:
1. Where they are located
2. What hiking intensity they are looking for
You will then provide three suggestions for nearby hikes that vary in length after you get that information.
You will also share an interesting fact about the local nature on the hikes when making a recommendation.
"""}}};

// Create a new Google AI model client.
var genai = new GoogleAI(apiKey);
var model = genai.GenerativeModel(model: Model.Gemini15Pro, systemInstruction: systemPrompt);

// == Starting the conversation ==========
string userGreeting = """
Hi!
Apparently you can help me find a hike that I will like?
""";
Console.WriteLine($"\nUser >>> {userGreeting}");

var response = await model.GenerateContent(userGreeting);
Console.WriteLine($"\nGemini >>> {response.Text}");

// == Providing the user's request ==========
var hikeRequest =
"""
I live in Mauritius area and would like an easy hike. I don't mind driving a bit to get there.
I don't want the hike to be over 15 kilometers round trip. I'd consider a point-to-point hike.
I want the hike to be as isolated as possible. I don't want to see many people.
I would like it to be as bug free as possible.
""";
Console.WriteLine($"\nUser >>> {hikeRequest}");

// == Getting the response from the AI model ==========
response = await model.GenerateContent(hikeRequest);
Console.WriteLine($"\nGemini >>> {response.Text}");
3 changes: 3 additions & 0 deletions samples/Console.HikerAI/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# HikerAI

This sample demonstrates how to use Gemini AI with a `gemini-1.5-pro` model, from a .NET 8.0 console application. Get a hiking recommendation from the AI model. It consists of a console application, running locally, that will send request to the Gemini API deployed in your Azure subscription.

0 comments on commit 96a4602

Please sign in to comment.