Skip to content

Commit a5bbca3

Browse files
author
Dustin Updyke
committed
api interface work
1 parent 8865ee5 commit a5bbca3

File tree

15 files changed

+183
-150
lines changed

15 files changed

+183
-150
lines changed

src/Ghosts.Api/Areas/Animator/Infrastructure/Animations/AnimationDefinitions/Chat/ChatClient.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Text.Json;
1111
using System.Threading.Tasks;
1212
using ghosts.api.Areas.Animator.Infrastructure.Animations.AnimationDefinitions.Chat.Mattermost;
13+
using ghosts.api.Areas.Animator.Infrastructure.ContentServices;
1314
using ghosts.api.Areas.Animator.Infrastructure.ContentServices.Ollama;
1415
using ghosts.api.Areas.Animator.Infrastructure.Extensions;
1516
using ghosts.api.Areas.Animator.Infrastructure.Models;
@@ -27,12 +28,14 @@ public class ChatClient
2728
private readonly HttpClient _client;
2829
private string _token;
2930
private string UserId { get; set; }
31+
private IFormatterService _formatterService;
3032

31-
public ChatClient(ChatJobConfiguration config)
33+
public ChatClient(ChatJobConfiguration config, IFormatterService formatterService)
3234
{
3335
_configuration = config;
3436
this._baseUrl = _configuration.Chat.BaseUrl;
3537
this._client = new HttpClient();
38+
this._formatterService = formatterService;
3639
}
3740

3841
private async Task<User> AdminLogin()
@@ -406,7 +409,7 @@ private async Task<string> ExecuteRequest(HttpRequestMessage request)
406409
}
407410
}
408411

409-
public async Task Step(OllamaConnectorService llm, Random random, IEnumerable<NpcRecord> agents)
412+
public async Task Step(Random random, IEnumerable<NpcRecord> agents)
410413
{
411414
await this.AdminLogin();
412415

@@ -425,11 +428,11 @@ await this.CreateUser(new UserCreate
425428
});
426429
}
427430

428-
await this.StepEx(llm, random, username, _configuration.Chat.DefaultUserPassword);
431+
await this.StepEx(random, username, _configuration.Chat.DefaultUserPassword);
429432
}
430433
}
431434

432-
private async Task StepEx(OllamaConnectorService llm, Random random, string username, string password)
435+
private async Task StepEx(Random random, string username, string password)
433436
{
434437
_log.Trace($"Managing {username}...");
435438

@@ -542,7 +545,7 @@ private async Task StepEx(OllamaConnectorService llm, Random random, string user
542545
respondingTo = history.UserName;
543546
}
544547

545-
var message = await llm.ExecuteQuery(prompt);
548+
var message = await this._formatterService.ExecuteQuery(prompt);
546549

547550
message = message.Clean(this._configuration.Replacements, random);
548551
if (!string.IsNullOrEmpty(respondingTo))

src/Ghosts.Api/Areas/Animator/Infrastructure/Animations/AnimationDefinitions/ChatJob.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Ghosts.Animator.Extensions;
99
using ghosts.api.Areas.Animator.Hubs;
1010
using ghosts.api.Areas.Animator.Infrastructure.Animations.AnimationDefinitions.Chat;
11+
using ghosts.api.Areas.Animator.Infrastructure.ContentServices;
1112
using ghosts.api.Areas.Animator.Infrastructure.ContentServices.Ollama;
1213
using ghosts.api.Areas.Animator.Infrastructure.Models;
1314
using Ghosts.Api.Infrastructure;
@@ -27,6 +28,7 @@ public class ChatJob
2728
private readonly ChatClient _chatClient;
2829
private readonly int _currentStep;
2930
private CancellationToken _cancellationToken;
31+
private IFormatterService _formatterService;
3032

3133
public ChatJob(ApplicationSettings configuration, IServiceScopeFactory scopeFactory, Random random,
3234
IHubContext<ActivityHub> activityHubContext, CancellationToken cancellationToken)
@@ -44,8 +46,10 @@ public ChatJob(ApplicationSettings configuration, IServiceScopeFactory scopeFact
4446
var chatConfiguration = JsonSerializer.Deserialize<ChatJobConfiguration>(File.ReadAllText("config/chat.json"),
4547
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? throw new InvalidOperationException();
4648

47-
var llm = new OllamaConnectorService(_configuration.AnimatorSettings.Animations.Chat.ContentEngine);
48-
this._chatClient = new ChatClient(chatConfiguration);
49+
this._formatterService =
50+
new ContentCreationService(_configuration.AnimatorSettings.Animations.Chat.ContentEngine).FormatterService;
51+
52+
this._chatClient = new ChatClient(chatConfiguration, this._formatterService);
4953

5054
while (!_cancellationToken.IsCancellationRequested)
5155
{
@@ -55,17 +59,17 @@ public ChatJob(ApplicationSettings configuration, IServiceScopeFactory scopeFact
5559
return;
5660
}
5761

58-
this.Step(llm, random, chatConfiguration);
59-
Thread.Sleep(this._configuration.AnimatorSettings.Animations.SocialSharing.TurnLength);
62+
this.Step(random, chatConfiguration);
63+
Thread.Sleep(this._configuration.AnimatorSettings.Animations.Chat.TurnLength);
6064

6165
this._currentStep++;
6266
}
6367
}
6468

65-
private async void Step(OllamaConnectorService llm, Random random, ChatJobConfiguration chatConfiguration)
69+
private async void Step(Random random, ChatJobConfiguration chatConfiguration)
6670
{
6771
_log.Trace("Executing a chat step...");
6872
var agents = this._context.Npcs.ToList().Shuffle(_random).Take(chatConfiguration.Chat.AgentsPerBatch);
69-
await this._chatClient.Step(llm, random, agents);
73+
await this._chatClient.Step(random, agents);
7074
}
7175
}

src/Ghosts.Api/Areas/Animator/Infrastructure/Animations/AnimationDefinitions/SocialSharingJob.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class SocialSharingJob
3333
private readonly CancellationToken _cancellationToken;
3434
private readonly ApplicationDbContext _context;
3535
private readonly IMachineUpdateService _updateService;
36+
private readonly IFormatterService _formatterService;
3637

3738
public SocialSharingJob(ApplicationSettings configuration, IServiceScopeFactory scopeFactory, Random random,
3839
IHubContext<ActivityHub> activityHubContext, CancellationToken cancellationToken)
@@ -48,6 +49,9 @@ public SocialSharingJob(ApplicationSettings configuration, IServiceScopeFactory
4849

4950
this._cancellationToken = cancellationToken;
5051
this._updateService = innerScope.ServiceProvider.GetRequiredService<IMachineUpdateService>();
52+
53+
_formatterService =
54+
new ContentCreationService(_configuration.AnimatorSettings.Animations.Chat.ContentEngine).FormatterService;
5155

5256
if (!_configuration.AnimatorSettings.Animations.SocialSharing.IsInteracting)
5357
{
@@ -84,9 +88,6 @@ private async void Step()
8488
{
8589
_log.Trace("Social sharing step proceeding...");
8690

87-
var contentService =
88-
new ContentCreationService(_configuration.AnimatorSettings.Animations.SocialSharing.ContentEngine);
89-
9091
//take some random NPCs
9192
var activities = new List<NpcActivity>();
9293
var rawAgents = this._context.Npcs.ToList();
@@ -102,7 +103,7 @@ private async void Step()
102103
foreach (var agent in agents)
103104
{
104105
_log.Trace($"Processing agent {agent.NpcProfile.Email}...");
105-
var tweetText = await contentService.GenerateTweet(agent);
106+
var tweetText = await this._formatterService.GenerateTweet(agent);
106107
if (string.IsNullOrEmpty(tweetText))
107108
{
108109
_log.Trace($"Content service generated no payload...");

src/Ghosts.Api/Areas/Animator/Infrastructure/Animations/AnimationsManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public Task StopAsync(CancellationToken cancellationToken)
109109
this._socialGraphJobCancellationTokenSource.Cancel();
110110
this._socialGraphJobThread?.Join();
111111
this.RemoveJob("SOCIALGRAPH");
112+
this._socialGraphJobCancellationTokenSource = new CancellationTokenSource();
112113
}
113114
catch
114115
{
@@ -120,6 +121,7 @@ public Task StopAsync(CancellationToken cancellationToken)
120121
this._socialSharingJobCancellationTokenSource.Cancel();
121122
this._socialSharingJobThread?.Join();
122123
this.RemoveJob("SOCIALSHARING");
124+
this._socialGraphJobCancellationTokenSource = new CancellationTokenSource();
123125
}
124126
catch
125127
{
@@ -131,6 +133,7 @@ public Task StopAsync(CancellationToken cancellationToken)
131133
this._socialSharingJobCancellationTokenSource.Cancel();
132134
this._socialBeliefsJobThread?.Join();
133135
this.RemoveJob("SOCIALBELIEF");
136+
this._socialGraphJobCancellationTokenSource = new CancellationTokenSource();
134137
}
135138
catch
136139
{
@@ -142,6 +145,7 @@ public Task StopAsync(CancellationToken cancellationToken)
142145
this._chatJobJobCancellationTokenSource.Cancel();
143146
this._chatJobThread?.Join();
144147
this.RemoveJob("CHAT");
148+
this._socialGraphJobCancellationTokenSource = new CancellationTokenSource();
145149
}
146150
catch
147151
{
@@ -153,6 +157,7 @@ public Task StopAsync(CancellationToken cancellationToken)
153157
this._fullAutonomyCancellationTokenSource.Cancel();
154158
this._fullAutonomyJobThread?.Join();
155159
this.RemoveJob("FULLAUTONOMY");
160+
this._socialGraphJobCancellationTokenSource = new CancellationTokenSource();
156161
}
157162
catch
158163
{
@@ -181,22 +186,27 @@ public Task StopJob(string jobId)
181186
case "SOCIALGRAPH":
182187
this._socialGraphJobCancellationTokenSource.Cancel();
183188
this._socialGraphJobThread?.Join();
189+
this._socialGraphJobCancellationTokenSource = new CancellationTokenSource();
184190
break;
185191
case "SOCIALSHARING":
186192
this._socialSharingJobCancellationTokenSource.Cancel();
187193
this._socialSharingJobThread?.Join();
194+
this._socialSharingJobCancellationTokenSource = new CancellationTokenSource();
188195
break;
189196
case "SOCIALBELIEFS":
190197
this._socialSharingJobCancellationTokenSource.Cancel();
191198
this._socialBeliefsJobThread?.Join();
199+
this._socialSharingJobCancellationTokenSource = new CancellationTokenSource();
192200
break;
193201
case "CHAT":
194202
this._chatJobJobCancellationTokenSource.Cancel();
195203
this._chatJobThread?.Join();
204+
this._chatJobJobCancellationTokenSource = new CancellationTokenSource();
196205
break;
197206
case "FULLAUTONOMY":
198207
this._fullAutonomyCancellationTokenSource.Cancel();
199208
this._fullAutonomyJobThread?.Join();
209+
this._chatJobJobCancellationTokenSource = new CancellationTokenSource();
200210
break;
201211
}
202212

Lines changed: 15 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
// Copyright 2017 Carnegie Mellon University. All Rights Reserved. See LICENSE.md file for terms.
22

33
using System;
4-
using System.Text.RegularExpressions;
54
using System.Threading.Tasks;
6-
using ghosts.api.Areas.Animator.Infrastructure.ContentServices.Native;
75
using ghosts.api.Areas.Animator.Infrastructure.ContentServices.Ollama;
86
using ghosts.api.Areas.Animator.Infrastructure.ContentServices.OpenAi;
97
using ghosts.api.Areas.Animator.Infrastructure.ContentServices.Shadows;
108
using ghosts.api.Areas.Animator.Infrastructure.Models;
119
using Ghosts.Api.Infrastructure;
12-
using Ghosts.Api.Infrastructure.Extensions;
1310
using NLog;
1411

1512
namespace ghosts.api.Areas.Animator.Infrastructure.ContentServices;
@@ -21,6 +18,7 @@ public class ContentCreationService
2118
private OpenAiFormatterService _openAiFormatterService;
2219
private OllamaFormatterService _ollamaFormatterService;
2320
private ShadowsFormatterService _shadowsFormatterService;
21+
public IFormatterService FormatterService;
2422

2523
public ContentCreationService(ApplicationSettings.AnimatorSettingsDetail.ContentEngineSettings configuration)
2624
{
@@ -31,124 +29,36 @@ public ContentCreationService(ApplicationSettings.AnimatorSettingsDetail.Content
3129
configuration.Model;
3230

3331
if (_configuration.Source.ToLower() == "openai" && this._openAiFormatterService.IsReady)
32+
{
3433
_openAiFormatterService = new OpenAiFormatterService();
34+
this.FormatterService = _openAiFormatterService;
35+
}
3536
else if (_configuration.Source.ToLower() == "ollama")
37+
{
3638
_ollamaFormatterService = new OllamaFormatterService(_configuration);
39+
this.FormatterService = _ollamaFormatterService;
40+
}
3741
else if (_configuration.Source.ToLower() == "shadows")
42+
{
3843
_shadowsFormatterService = new ShadowsFormatterService(_configuration);
44+
this.FormatterService = _shadowsFormatterService;
45+
}
3946

4047
_log.Trace($"Content service configured for {_configuration.Source} on {_configuration.Host} running {_configuration.Model}");
4148
}
4249

4350
public async Task<string> GenerateNextAction(NpcRecord agent, string history)
4451
{
45-
var nextAction = string.Empty;
46-
try
47-
{
48-
if (_configuration.Source.ToLower() == "openai" && this._openAiFormatterService.IsReady)
49-
{
50-
nextAction = await this._openAiFormatterService.GenerateNextAction(agent, history).ConfigureAwait(false);
51-
}
52-
else if (_configuration.Source.ToLower() == "ollama")
53-
{
54-
nextAction = await this._ollamaFormatterService.GenerateNextAction(agent, history);
55-
}
56-
else if (_configuration.Source.ToLower() == "shadows")
57-
{
58-
nextAction = await this._shadowsFormatterService.GenerateNextAction(agent, history);
59-
}
60-
61-
_log.Info($"{agent.NpcProfile.Name}'s next action is: {nextAction}");
62-
}
63-
catch (Exception e)
64-
{
65-
_log.Error(e);
66-
}
52+
var nextAction = await this.FormatterService.GenerateNextAction(agent, history);
53+
_log.Info($"{agent.NpcProfile.Name}'s next action is: {nextAction}");
6754
return nextAction;
6855
}
6956

70-
public async Task<string> GenerateTweet(NpcRecord agent)
57+
public async Task<string> GenerateTweet(NpcRecord npc)
7158
{
72-
string tweetText = null;
73-
74-
try
75-
{
76-
if (_configuration.Source.ToLower() == "openai" && this._openAiFormatterService.IsReady)
77-
{
78-
tweetText = await this._openAiFormatterService.GenerateTweet(agent).ConfigureAwait(false);
79-
}
80-
else if (_configuration.Source.ToLower() == "ollama")
81-
{
82-
var tries = 0;
83-
while (string.IsNullOrEmpty(tweetText))
84-
{
85-
tweetText = await this._ollamaFormatterService.GenerateTweet(agent);
86-
tries++;
87-
if (tries > 5)
88-
return null;
89-
}
90-
91-
var regArray = new [] {"\"activities\": \\[\"([^\"]+)\"", "\"activity\": \"([^\"]+)\"", "'activities': \\['([^\\']+)'\\]", "\"activities\": \\[\"([^\\']+)'\\]"} ;
92-
93-
foreach (var reg in regArray)
94-
{
95-
var match = Regex.Match(tweetText,reg);
96-
if (match.Success)
97-
{
98-
// Extract the activity
99-
tweetText = match.Groups[1].Value;
100-
break;
101-
}
102-
}
103-
}
104-
else if (_configuration.Source.ToLower() == "shadows")
105-
{
106-
var tries = 0;
107-
while (string.IsNullOrEmpty(tweetText))
108-
{
109-
tweetText = await this._shadowsFormatterService.GenerateTweet(agent);
110-
tries++;
111-
if (tries > 5)
112-
return null;
113-
}
114-
115-
var regArray = new [] {"\"activities\": \\[\"([^\"]+)\"", "\"activity\": \"([^\"]+)\"", "'activities': \\['([^\\']+)'\\]", "\"activities\": \\[\"([^\\']+)'\\]"} ;
116-
117-
foreach (var reg in regArray)
118-
{
119-
var match = Regex.Match(tweetText,reg);
120-
if (match.Success)
121-
{
122-
// Extract the activity
123-
tweetText = match.Groups[1].Value;
124-
break;
125-
}
126-
}
127-
}
128-
129-
while (string.IsNullOrEmpty(tweetText))
130-
{
131-
tweetText = NativeContentFormatterService.GenerateTweet(agent);
132-
}
133-
134-
tweetText = tweetText.ReplaceDoubleQuotesWithSingleQuotes(); // else breaks csv file, //TODO should replace this with a proper csv library
135-
136-
tweetText = Clean(tweetText);
137-
138-
_log.Info($"{agent.NpcProfile.Name} said: {tweetText}");
139-
}
140-
catch (Exception e)
141-
{
142-
_log.Info(e);
143-
}
59+
var tweetText = await this.FormatterService.GenerateTweet(npc);
60+
_log.Info($"{npc.NpcProfile.Name} said: {tweetText}");
14461
return tweetText;
14562
}
14663

147-
private string Clean(string raw)
148-
{
149-
raw = raw.Replace("`", "");
150-
raw = raw.Replace("\"", "");
151-
raw = raw.Replace("'", "");
152-
return raw;
153-
}
15464
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.Threading.Tasks;
2+
3+
namespace ghosts.api.Areas.Animator.Infrastructure.ContentServices;
4+
5+
public interface IContentService
6+
{
7+
Task<string> ExecuteQuery(string prompt);
8+
}

0 commit comments

Comments
 (0)