Compare commits

...
This repository has been archived on 2023-11-16. You can view files and clone it, but cannot push or open issues or pull requests.

5 Commits
main ... llmapi

Author SHA1 Message Date
6e0421bc65 fix 2023-09-24 12:57:19 +02:00
b61d649847 stuff 2023-09-23 21:51:28 +02:00
820d2f0d76 completed basic api interface for continuous communication 2023-09-23 15:33:16 +02:00
cb5842a32b added chatgpt3.5 api 2023-09-23 12:00:41 +02:00
ac8b51031b batman 2023-09-23 11:21:46 +02:00
18 changed files with 404 additions and 0 deletions

35
LLMApi/.vscode/launch.json vendored Normal file
View 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
View 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
View 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"
}
]
}

View 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());
}
}
}

View File

@ -0,0 +1,6 @@
namespace LLMApi.Data.Contracts.ChatGpt3_5;
public record ChatCompletionRequest(
string Model,
Message[] Messages
);

View 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
);

View File

@ -0,0 +1,7 @@
namespace LLMApi.Data.Contracts.ChatGpt3_5;
public record Choice(
int Index,
Message Message,
string FinishReason
);

View File

@ -0,0 +1,6 @@
namespace LLMApi.Data.Contracts.ChatGpt3_5;
public record Message(
string Role,
string? Content
);

View File

@ -0,0 +1,7 @@
namespace LLMApi.Data.Contracts.ChatGpt3_5;
public record Usage(
int PromptTokens,
int CompletionTokens,
int TotalTokens
);

View 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
);

View 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
View 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
View 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();

View 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"
}
}
}
}

View 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);
}
}

View 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);
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

11
LLMApi/appsettings.json Normal file
View 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."
}