Introduction
In the rapidly evolving landscape of healthcare, efficient and accurate documentation is paramount. From patient histories to treatment plans, every detail matters. However, the sheer volume of information can overwhelm healthcare professionals, leading to errors or oversights. Fortunately, advancements in artificial intelligence (AI) are offering solutions to streamline this process.
One such solution is demonstrated through a Multi-Turn Chat Application, which leverages Google's AI Platform to enhance medical documentation. Let's delve into how this application works and its potential benefits for healthcare providers.
The Multi-Turn Chat Application
using System;
using System.Threading.Tasks;
using Google.Cloud.AIPlatform.V1;
using System.Collections.Generic;
using System.Linq;
using static Google.Rpc.Context.AttributeContext.Types;
public class MultiTurnChatSample
{
public async Task<string> GenerateContent(
string projectId = "YOUR_PROJECT_ID",
string location = "YOUR_LOCATION",
string publisher = "YOUR_PUBLISHER",
string model = "YOUR_MODEL",
string jsonKeyFilePath = "YOUR_JSON_KEY_FILE_PATH"
)
{
// Specify the path to your service account key file
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", jsonKeyFilePath);
// Create a chat session to keep track of the context
ChatSession chatSession = new ChatSession($"projects/{projectId}/locations/{location}/publishers/{publisher}/models/{model}", location);
// Input string to be divided into chunks
string input = "YOUR_INPUT_STRING"; // Update with your input string
// Split the input string into chunks of 20 words
List<string> chunks = SplitIntoChunks(input, 20);
string formattedResponse = "";
int i = 1;
// Iterate through each chunk and process
foreach (var chunk in chunks)
{
Console.WriteLine($"\nInput Segment-{i} : {chunk}");
// Send the chunk to Gemini and receive the response
string response = await chatSession.SendMessageAsync($"correct spelling and formatting the text: {chunk}");
Console.WriteLine($"Out Segment -{i}: {response}");
i++;
// Append the response to the formattedResponse string
formattedResponse += response + "\n";
}
Console.WriteLine("====================================");
Console.WriteLine($"Final result {formattedResponse}");
return formattedResponse;
}
// Helper method to split the input string into chunks of specified size
private List<string> SplitIntoChunks(string input, int chunkSize)
{
List<string> chunks = new List<string>();
string[] words = input.Split(' ');
for (int i = 0; i < words.Length; i += chunkSize)
{
chunks.Add(string.Join(" ", words.Skip(i).Take(chunkSize)));
}
return chunks;
}
private class ChatSession
{
private readonly string _modelPath;
private readonly PredictionServiceClient _predictionServiceClient;
public ChatSession(string modelPath, string location)
{
_modelPath = modelPath;
// Create a prediction service client
_predictionServiceClient = new PredictionServiceClientBuilder
{
Endpoint = $"{location}-aiplatform.googleapis.com"
}.Build();
}
public async Task<string> SendMessageAsync(string prompt)
{
// Create a request to generate content
var generateContentRequest = new GenerateContentRequest
{
Model = _modelPath,
Contents =
{
new Content
{
Role = "USER",
Parts = { new Part { Text = prompt } }
}
},
GenerationConfig = new GenerationConfig
{
Temperature = 0,
TopP = 0,
MaxOutputTokens = 2048
}
};
// Make a non-streaming request, get a response
GenerateContentResponse response = await _predictionServiceClient.GenerateContentAsync(generateContentRequest);
// Return the text from the response
return response.Candidates[0].Content.Parts[0].Text;
}
}
static async Task Main(string[] args)
{
// Specify the path to your service account key file
string jsonKeyFilePath = "YOUR_JSON_KEY_FILE_PATH";
var chatSample = new MultiTurnChatSample();
var response = await chatSample.GenerateContent(jsonKeyFilePath: jsonKeyFilePath);
Console.WriteLine(response);
}
}

Join the conversation! Your thoughts help the community grow.