Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
6e0421bc65 | |||
b61d649847 | |||
820d2f0d76 | |||
cb5842a32b | |||
ac8b51031b |
35
LLMApi/.vscode/launch.json
vendored
Normal file
35
LLMApi/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
// Use IntelliSense to find out which attributes exist for C# debugging
|
||||||
|
// Use hover for the description of the existing attributes
|
||||||
|
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md.
|
||||||
|
"name": ".NET Core Launch (web)",
|
||||||
|
"type": "coreclr",
|
||||||
|
"request": "launch",
|
||||||
|
"preLaunchTask": "build",
|
||||||
|
// If you have changed target frameworks, make sure to update the program path.
|
||||||
|
"program": "${workspaceFolder}/bin/Debug/net7.0/LLMApi.dll",
|
||||||
|
"args": [],
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"stopAtEntry": false,
|
||||||
|
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
|
||||||
|
"serverReadyAction": {
|
||||||
|
"action": "openExternally",
|
||||||
|
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
|
||||||
|
},
|
||||||
|
"env": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
},
|
||||||
|
"sourceFileMap": {
|
||||||
|
"/Views": "${workspaceFolder}/Views"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": ".NET Core Attach",
|
||||||
|
"type": "coreclr",
|
||||||
|
"request": "attach"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
12
LLMApi/.vscode/settings.json
vendored
Normal file
12
LLMApi/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"files.exclude": {
|
||||||
|
"**/.git": true,
|
||||||
|
"**/.svn": true,
|
||||||
|
"**/.hg": true,
|
||||||
|
"**/CVS": true,
|
||||||
|
"**/.DS_Store": true,
|
||||||
|
"**/Thumbs.db": true,
|
||||||
|
"**/bin/": true,
|
||||||
|
"**/obj/": true,
|
||||||
|
}
|
||||||
|
}
|
41
LLMApi/.vscode/tasks.json
vendored
Normal file
41
LLMApi/.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "build",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"build",
|
||||||
|
"${workspaceFolder}/LLMApi.csproj",
|
||||||
|
"/property:GenerateFullPaths=true",
|
||||||
|
"/consoleloggerparameters:NoSummary"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "publish",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"publish",
|
||||||
|
"${workspaceFolder}/LLMApi.csproj",
|
||||||
|
"/property:GenerateFullPaths=true",
|
||||||
|
"/consoleloggerparameters:NoSummary"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "watch",
|
||||||
|
"command": "dotnet",
|
||||||
|
"type": "process",
|
||||||
|
"args": [
|
||||||
|
"watch",
|
||||||
|
"run",
|
||||||
|
"--project",
|
||||||
|
"${workspaceFolder}/LLMApi.csproj"
|
||||||
|
],
|
||||||
|
"problemMatcher": "$msCompile"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
58
LLMApi/Controllers/LLMController.cs
Normal file
58
LLMApi/Controllers/LLMController.cs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
using LLMApi.Data.Contracts.ChatGpt3_5;
|
||||||
|
using LLMApi.Data.Contracts.Game.ChatGpt3_5;
|
||||||
|
using LLMApi.Services;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace LLMApi.Controllers;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("[controller]")]
|
||||||
|
public class LLMController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly ILlmApiService _apiService;
|
||||||
|
|
||||||
|
public LLMController(ILlmApiService apiService)
|
||||||
|
{
|
||||||
|
_apiService = apiService;
|
||||||
|
}
|
||||||
|
|
||||||
|
// [HttpGet(nameof(AnswerToPrompt))]
|
||||||
|
// public async Task<ActionResult<Choice>> AnswerToPrompt(string prompt = "Hello!")
|
||||||
|
// {
|
||||||
|
// try
|
||||||
|
// {
|
||||||
|
// var choices = await _apiService.GetAnswerToPrompt(prompt);
|
||||||
|
|
||||||
|
// return Ok(choices);
|
||||||
|
// }
|
||||||
|
// catch (Exception ex)
|
||||||
|
// {
|
||||||
|
// _logger.LogError(ex, null, Array.Empty<object>());
|
||||||
|
// return StatusCode(500, ex.ToString());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
[HttpGet(nameof(Answer))]
|
||||||
|
public async Task<ActionResult<AnswerResponse>> Answer(AnswerRequest answerRequest)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
System.Console.WriteLine(JsonConvert.SerializeObject(answerRequest));
|
||||||
|
System.Console.WriteLine(">");
|
||||||
|
|
||||||
|
var answerResponse = await _apiService.GetAnswer(answerRequest);
|
||||||
|
|
||||||
|
System.Console.WriteLine(JsonConvert.SerializeObject(answerResponse));
|
||||||
|
System.Console.WriteLine();
|
||||||
|
System.Console.WriteLine();
|
||||||
|
|
||||||
|
return Ok(answerResponse);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.ToString());
|
||||||
|
return StatusCode(500, ex.ToString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
namespace LLMApi.Data.Contracts.ChatGpt3_5;
|
||||||
|
|
||||||
|
public record ChatCompletionRequest(
|
||||||
|
string Model,
|
||||||
|
Message[] Messages
|
||||||
|
);
|
10
LLMApi/Data/Contracts/ChatGpt3_5/ChatCompletionResponse.cs
Normal file
10
LLMApi/Data/Contracts/ChatGpt3_5/ChatCompletionResponse.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
namespace LLMApi.Data.Contracts.ChatGpt3_5;
|
||||||
|
|
||||||
|
public record ChatCompletionResponse(
|
||||||
|
string Id,
|
||||||
|
string Object,
|
||||||
|
int Created,
|
||||||
|
string Model,
|
||||||
|
Choice[] Choices,
|
||||||
|
Usage Usage
|
||||||
|
);
|
7
LLMApi/Data/Contracts/ChatGpt3_5/Choice.cs
Normal file
7
LLMApi/Data/Contracts/ChatGpt3_5/Choice.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace LLMApi.Data.Contracts.ChatGpt3_5;
|
||||||
|
|
||||||
|
public record Choice(
|
||||||
|
int Index,
|
||||||
|
Message Message,
|
||||||
|
string FinishReason
|
||||||
|
);
|
6
LLMApi/Data/Contracts/ChatGpt3_5/Message.cs
Normal file
6
LLMApi/Data/Contracts/ChatGpt3_5/Message.cs
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
namespace LLMApi.Data.Contracts.ChatGpt3_5;
|
||||||
|
|
||||||
|
public record Message(
|
||||||
|
string Role,
|
||||||
|
string? Content
|
||||||
|
);
|
7
LLMApi/Data/Contracts/ChatGpt3_5/Usage.cs
Normal file
7
LLMApi/Data/Contracts/ChatGpt3_5/Usage.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace LLMApi.Data.Contracts.ChatGpt3_5;
|
||||||
|
|
||||||
|
public record Usage(
|
||||||
|
int PromptTokens,
|
||||||
|
int CompletionTokens,
|
||||||
|
int TotalTokens
|
||||||
|
);
|
8
LLMApi/Data/Contracts/Game/ChatGpt3_5/AnswerRequest.cs
Normal file
8
LLMApi/Data/Contracts/Game/ChatGpt3_5/AnswerRequest.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
using LLMApi.Data.Contracts.ChatGpt3_5;
|
||||||
|
|
||||||
|
namespace LLMApi.Data.Contracts.Game.ChatGpt3_5;
|
||||||
|
|
||||||
|
public record AnswerRequest(
|
||||||
|
Message[]? Context,
|
||||||
|
string Prompt
|
||||||
|
);
|
8
LLMApi/Data/Contracts/Game/ChatGpt3_5/AnswerResponse.cs
Normal file
8
LLMApi/Data/Contracts/Game/ChatGpt3_5/AnswerResponse.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
using LLMApi.Data.Contracts.ChatGpt3_5;
|
||||||
|
|
||||||
|
namespace LLMApi.Data.Contracts.Game.ChatGpt3_5;
|
||||||
|
|
||||||
|
public record AnswerResponse(
|
||||||
|
Message[] Context,
|
||||||
|
IEnumerable<Choice> Choices
|
||||||
|
);
|
15
LLMApi/LLMApi.csproj
Normal file
15
LLMApi/LLMApi.csproj
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.11" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
30
LLMApi/Program.cs
Normal file
30
LLMApi/Program.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
using LLMApi.Services;
|
||||||
|
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
// Add services to the container.
|
||||||
|
|
||||||
|
builder.Services.AddControllers();
|
||||||
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
|
builder.Services.AddSingleton<HttpClient>();
|
||||||
|
builder.Services.AddSingleton<ILlmApiService, ChatGpt3_5Service>();
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Configure the HTTP request pipeline.
|
||||||
|
if (app.Environment.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.MapControllers();
|
||||||
|
|
||||||
|
app.Run();
|
41
LLMApi/Properties/launchSettings.json
Normal file
41
LLMApi/Properties/launchSettings.json
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:17280",
|
||||||
|
"sslPort": 44333
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"profiles": {
|
||||||
|
"http": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "http://localhost:5246",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"https": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "https://localhost:7113;http://localhost:5246",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"IIS Express": {
|
||||||
|
"commandName": "IISExpress",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
90
LLMApi/Services/ChatGpt3_5.cs
Normal file
90
LLMApi/Services/ChatGpt3_5.cs
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
using LLMApi.Data.Contracts.ChatGpt3_5;
|
||||||
|
using LLMApi.Data.Contracts.Game.ChatGpt3_5;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
|
|
||||||
|
namespace LLMApi.Services;
|
||||||
|
|
||||||
|
public class ChatGpt3_5Service : ILlmApiService
|
||||||
|
{
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
private static readonly JsonSerializerSettings jsonDeserializerSettings = new()
|
||||||
|
{
|
||||||
|
ContractResolver = new DefaultContractResolver()
|
||||||
|
{
|
||||||
|
NamingStrategy = new SnakeCaseNamingStrategy()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
private const string VERSION_IDENTIFIER = "gpt-3.5-turbo";
|
||||||
|
|
||||||
|
private readonly string _llmRuleset;
|
||||||
|
|
||||||
|
public ChatGpt3_5Service(HttpClient client, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
_client = client;
|
||||||
|
|
||||||
|
_client.BaseAddress = new Uri("https://api.openai.com");
|
||||||
|
|
||||||
|
var api_key = configuration["api_key"];
|
||||||
|
|
||||||
|
_client.DefaultRequestHeaders.Add("Authorization", $"Bearer {api_key}");
|
||||||
|
|
||||||
|
_llmRuleset = configuration["llm_ruleset"] ?? "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// public async Task<IEnumerable<Choice>> GetAnswerToPrompt(string prompt)
|
||||||
|
// {
|
||||||
|
// var requestContract = new ChatCompletionRequest(VERSION_IDENTIFIER,
|
||||||
|
// new Message[]
|
||||||
|
// {
|
||||||
|
// new("system", "You are a helpful assistant."),
|
||||||
|
// new("user", prompt),
|
||||||
|
// });
|
||||||
|
|
||||||
|
// var responseMessage = await _client.PostAsJsonAsync("/v1/chat/completions", requestContract);
|
||||||
|
|
||||||
|
// var responseContent = await responseMessage.Content.ReadAsStringAsync();
|
||||||
|
|
||||||
|
// var response = JsonConvert.DeserializeObject<ChatCompletionResponse>(responseContent, jsonDeserializerSettings);
|
||||||
|
|
||||||
|
// ArgumentNullException.ThrowIfNull(response);
|
||||||
|
|
||||||
|
// var choices = response.Choices.OrderBy(c => c.Index);
|
||||||
|
|
||||||
|
// return choices;
|
||||||
|
// }
|
||||||
|
|
||||||
|
public async Task<AnswerResponse> GetAnswer(AnswerRequest answerRequest)
|
||||||
|
{
|
||||||
|
var (context, prompt) = answerRequest;
|
||||||
|
|
||||||
|
Message[] newContext;
|
||||||
|
if (context is null)
|
||||||
|
{
|
||||||
|
newContext = new[]
|
||||||
|
{
|
||||||
|
new Message("system", _llmRuleset),
|
||||||
|
new Message("user", prompt)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
newContext = new List<Message>(context)
|
||||||
|
{
|
||||||
|
new("user", prompt)
|
||||||
|
}.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
var requestContract = new ChatCompletionRequest(VERSION_IDENTIFIER, newContext);
|
||||||
|
|
||||||
|
var responseMessage = await _client.PostAsJsonAsync("/v1/chat/completions", requestContract);
|
||||||
|
var responseContent = await responseMessage.Content.ReadAsStringAsync();
|
||||||
|
// var responseContent = await File.ReadAllTextAsync("/home/finn/Documents/pit_hackathon/GOA.git/llmapi/LLMApi/test.json");
|
||||||
|
var response = JsonConvert.DeserializeObject<ChatCompletionResponse>(responseContent, jsonDeserializerSettings);
|
||||||
|
|
||||||
|
ArgumentNullException.ThrowIfNull(response);
|
||||||
|
|
||||||
|
var choices = response.Choices.OrderBy(c => c.Index);
|
||||||
|
return new(newContext, choices);
|
||||||
|
}
|
||||||
|
}
|
11
LLMApi/Services/ILLMApiService.cs
Normal file
11
LLMApi/Services/ILLMApiService.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using LLMApi.Data.Contracts.ChatGpt3_5;
|
||||||
|
using LLMApi.Data.Contracts.Game.ChatGpt3_5;
|
||||||
|
|
||||||
|
namespace LLMApi.Services;
|
||||||
|
|
||||||
|
public interface ILlmApiService
|
||||||
|
{
|
||||||
|
// public Task<IEnumerable<Choice>> GetAnswerToPrompt(string prompt);
|
||||||
|
|
||||||
|
public Task<AnswerResponse> GetAnswer(AnswerRequest answerRequest);
|
||||||
|
}
|
8
LLMApi/appsettings.Development.json
Normal file
8
LLMApi/appsettings.Development.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
LLMApi/appsettings.json
Normal file
11
LLMApi/appsettings.json
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*",
|
||||||
|
"api_key": "",
|
||||||
|
"llm_ruleset": "You are the game master of a computer RPG. It uses Fallout's S.P.E.C.I.A.L. system. You won't mention the S.P.E.C.I.A.L. system or Fallout. You won't write any Dialogue for the player character, but instead let the player respond themselves, before proceeding. You won't mention any dialogue or interaction options, unless the player asks you for some. When the player dies, you will describe the player's death and write \"\" afterward, without the quotes. Once GAMEOVER is reached, the player cannot interact with anything anymore. The player may use items in their possession, but they can only use items, which they have previously received. Item's may be consumed, if appropriate. Consumed items are not within the possession of the player anymore. You will give the player a couple of items appropriate for their character at the beginning. A player should at least receive some form of consumable healing item and weapon at the beginning. When the player tries to perform an action, you should use the player's attributes to determine if the player succeeds."
|
||||||
|
}
|
Reference in New Issue
Block a user