Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions JobFlow.Business/Services/AiWriterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using JobFlow.Business.Services.ServiceInterfaces;
using Microsoft.Extensions.Options;
using OpenAI.Chat;
using System.ClientModel;

namespace JobFlow.Business.Services;

Expand Down Expand Up @@ -39,20 +40,33 @@ public async Task<Result<string>> DraftEstimateNotesAsync(Guid organizationId, s
Output only the notes text — no headers, no labels, no extra commentary.
""";

var client = new ChatClient(_openAiSettings.Model, _openAiSettings.ApiKey);

var messages = new List<ChatMessage>
try
{
new UserChatMessage(prompt)
};
var client = new ChatClient(_openAiSettings.Model, _openAiSettings.ApiKey);

var response = await client.CompleteChatAsync(messages, new ChatCompletionOptions
{
MaxOutputTokenCount = 200,
Temperature = 0.5f
});
var messages = new List<ChatMessage>
{
new UserChatMessage(prompt)
};

var response = await client.CompleteChatAsync(messages, new ChatCompletionOptions
{
MaxOutputTokenCount = 200,
Temperature = 0.5f
});

var notes = response.Value.Content[0].Text.Trim();
return Result.Success(notes);
var notes = response.Value.Content[0].Text.Trim();
return Result.Success(notes);
}
catch (ClientResultException ex)
{
return Result.Failure<string>(Error.Failure("AiWriter.ApiError",
$"The AI service returned an error (HTTP {ex.Status}). Check that your API key and model are configured correctly."));
}
catch (Exception)
{
return Result.Failure<string>(Error.Failure("AiWriter.Unavailable",
"The AI service is temporarily unavailable. Please try again."));
}
}
}
45 changes: 29 additions & 16 deletions JobFlow.Business/Services/SetupCompanionService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using JobFlow.Business.ConfigurationSettings;
using JobFlow.Business.ConfigurationSettings;
using JobFlow.Business.DI;
using JobFlow.Business.Services.ServiceInterfaces;
using JobFlow.Domain;
using JobFlow.Domain.Models;
using Microsoft.Extensions.Options;
using OpenAI.Chat;
using System.ClientModel;

namespace JobFlow.Business.Services;

Expand Down Expand Up @@ -45,7 +46,6 @@ public async Task<Result<string>> AskAsync(Guid organizationId, string sessionId
if (org is null)
return Result.Failure<string>(Error.NotFound("Organization.NotFound", "Organization not found."));

// Track the free-text ask as an analytics event
var ev = new SetupCompanionEvent
{
OrganizationId = organizationId,
Expand All @@ -59,22 +59,35 @@ public async Task<Result<string>> AskAsync(Guid organizationId, string sessionId

var systemPrompt = BuildSystemPrompt(org, currentRoute);

var client = new ChatClient(_openAiSettings.Model, _openAiSettings.ApiKey);

var messages = new List<ChatMessage>
try
{
new SystemChatMessage(systemPrompt),
new UserChatMessage(question)
};

var response = await client.CompleteChatAsync(messages, new ChatCompletionOptions
var client = new ChatClient(_openAiSettings.Model, _openAiSettings.ApiKey);

var messages = new List<ChatMessage>
{
new SystemChatMessage(systemPrompt),
new UserChatMessage(question)
};

var response = await client.CompleteChatAsync(messages, new ChatCompletionOptions
{
MaxOutputTokenCount = 400,
Temperature = 0.3f
});

var answer = response.Value.Content[0].Text;
return Result.Success(answer);
}
catch (ClientResultException ex)
{
MaxOutputTokenCount = 400,
Temperature = 0.3f
});

var answer = response.Value.Content[0].Text;
return Result.Success(answer);
return Result.Failure<string>(Error.Failure("Companion.ApiError",
$"The AI service returned an error (HTTP {ex.Status}). Check that your API key and model are configured correctly."));
}
catch (Exception)
{
return Result.Failure<string>(Error.Failure("Companion.Unavailable",
"The AI service is temporarily unavailable. Please try again."));
}
}

private static string BuildSystemPrompt(Organization org, string currentRoute)
Expand Down
Loading