-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25dd8e2
commit 6a78267
Showing
14 changed files
with
597 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<AssemblyName>ChatGPT.API.Framework</AssemblyName> | ||
<RootNamespace>ChatGPT.API.Framework</RootNamespace> | ||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> | ||
<Title>ChatGPT.API.Framework</Title> | ||
<Authors>LorisYounger</Authors> | ||
<Company>exLB.org</Company> | ||
<Description>This is the only .net Framework ChatGPT API You Can use</Description> | ||
<Copyright>exLB.org</Copyright> | ||
<PackageProjectUrl>https://github.com/LorisYounger/ChatGPT.API.Framework</PackageProjectUrl> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="..\ChatGPT.API.Framework\Completions.cs" /> | ||
<Compile Include="..\ChatGPT.API.Framework\Message.cs" /> | ||
<Compile Include="..\ChatGPT.API.Framework\Response.cs" /> | ||
<Compile Include="..\ChatGPT.API.Framework\ChatGPTClient.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="..\LICENSE"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
<None Include="..\README.md"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.4.33213.308 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatGPT.API.Framework", "ChatGPT.API.Framework\ChatGPT.API.Framework.csproj", "{C93B67C8-7F98-452B-B2CB-03D1F54CA244}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatGPT.API.Test", "ChatGPT.API.Test\ChatGPT.API.Test.csproj", "{7C30A416-042E-4228-A922-0C8FAB083CC4}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatGPT.API.Core", "ChatGPT.API.Core\ChatGPT.API.Core.csproj", "{299E61B2-C9C9-4492-9F5F-AD5DAD1095C7}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{C93B67C8-7F98-452B-B2CB-03D1F54CA244}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{C93B67C8-7F98-452B-B2CB-03D1F54CA244}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{C93B67C8-7F98-452B-B2CB-03D1F54CA244}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{C93B67C8-7F98-452B-B2CB-03D1F54CA244}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{7C30A416-042E-4228-A922-0C8FAB083CC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{7C30A416-042E-4228-A922-0C8FAB083CC4}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{7C30A416-042E-4228-A922-0C8FAB083CC4}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{7C30A416-042E-4228-A922-0C8FAB083CC4}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{299E61B2-C9C9-4492-9F5F-AD5DAD1095C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{299E61B2-C9C9-4492-9F5F-AD5DAD1095C7}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{299E61B2-C9C9-4492-9F5F-AD5DAD1095C7}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{299E61B2-C9C9-4492-9F5F-AD5DAD1095C7}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {3365A8C2-34FD-41AE-9A15-C30FED7CFCEF} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{C93B67C8-7F98-452B-B2CB-03D1F54CA244}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>ChatGPT.API.Framework</RootNamespace> | ||
<AssemblyName>ChatGPT.API.Framework</AssemblyName> | ||
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<Deterministic>true</Deterministic> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
<DocumentationFile>bin\Release\ChatGPT.API.Framework.xml</DocumentationFile> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> | ||
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath> | ||
</Reference> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Completions.cs" /> | ||
<Compile Include="Message.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
<Compile Include="Response.cs" /> | ||
<Compile Include="ChatGPTClient.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChatGPT.API.Framework | ||
{ | ||
/// <summary> | ||
/// ChatGPT Client | ||
/// </summary> | ||
public class ChatGPTClient | ||
{ | ||
public ChatGPTClient(string aPIKey) | ||
{ | ||
APIKey = aPIKey; | ||
} | ||
|
||
public ChatGPTClient() | ||
{ | ||
} | ||
|
||
|
||
/// <summary> | ||
/// YOUR_API_KEY | ||
/// </summary> | ||
public string APIKey { get; set; } | ||
/// <summary> | ||
/// Total Token Usage | ||
/// </summary> | ||
public long TotalTokensUsage { get; set; } | ||
/// <summary> | ||
/// a completion for the chat message | ||
/// </summary> | ||
public Dictionary<string, Completions> Completions { get; set; } = new Dictionary<string, Completions>(); | ||
|
||
/// <summary> | ||
/// Save as Json | ||
/// </summary> | ||
public string Save() => JsonConvert.SerializeObject(this); | ||
/// <summary> | ||
/// Load from Json | ||
/// </summary> | ||
public static ChatGPTClient Load(string json) => JsonConvert.DeserializeObject<ChatGPTClient>(json); | ||
/// <summary> | ||
/// Create A new Chat Completions | ||
/// </summary> | ||
public Completions CreateCompletions(string id, string systemmessages) | ||
{ | ||
var cp = new Completions(); | ||
cp.messages.Add(new Message() { role = Message.RoleType.system, content = systemmessages }); | ||
Completions.Add(id, cp); | ||
return cp; | ||
} | ||
/// <summary> | ||
/// Ask ChatGPT | ||
/// </summary> | ||
/// <returns></returns> | ||
public Response Ask(string id, string usermessage) | ||
{ | ||
if (!Completions.TryGetValue(id, out Completions cp)) | ||
{ | ||
cp = new Completions(); | ||
Completions.Add(id, cp); | ||
} | ||
cp.messages.Add(new Message() { role = Message.RoleType.user, content = usermessage }); | ||
|
||
var request = (HttpWebRequest)WebRequest.Create("https://api.openai.com/v1/chat/completions"); | ||
request.Method = "POST"; | ||
request.ContentType = "application/json";//ContentType | ||
request.Headers.Add("Authorization", "Bearer " + APIKey); | ||
var str = JsonConvert.SerializeObject(cp); | ||
byte[] byteData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(cp)); | ||
int length = byteData.Length; | ||
request.ContentLength = length; | ||
using (Stream writer = request.GetRequestStream()) | ||
{ | ||
writer.Write(byteData, 0, length); | ||
writer.Close(); | ||
} | ||
string responseString; | ||
using (var response = (HttpWebResponse)request.GetResponse()) | ||
{ | ||
responseString = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd(); | ||
} | ||
var rs = JsonConvert.DeserializeObject<Response>(responseString); | ||
cp.messages.Add(rs.GetMessage()); | ||
TotalTokensUsage += rs.usage.total_tokens; | ||
return rs; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChatGPT.API.Framework | ||
{ | ||
/// <summary> | ||
/// a completion for the chat message | ||
/// </summary> | ||
public class Completions | ||
{ | ||
/// <summary> | ||
/// ID of the model to use. Currently, only gpt-3.5-turbo and gpt-3.5-turbo-0301 are supported. | ||
/// </summary> | ||
public string model { get; set; } = "gpt-3.5-turbo"; | ||
/// <summary> | ||
/// What sampling temperature to use, between 0 and 2. | ||
/// Higher values like 0.8 will make the output more random, | ||
/// while lower values like 0.2 will make it more focused and deterministic. | ||
/// </summary> | ||
public double temperature { get; set; } = 1; | ||
/// <summary> | ||
/// The maximum number of tokens allowed for the generated answer. | ||
/// By default, the number of tokens the model can return will be (4096 - prompt tokens). | ||
/// </summary> | ||
public int max_tokens { get; set; } = 2048; | ||
/// <summary> | ||
/// Number between -2.0 and 2.0. | ||
/// Positive values penalize new tokens based on whether they appear in the text so far | ||
/// increasing the model's likelihood to talk about new topics. | ||
/// </summary> | ||
public double presence_penalty { get; set; } = 0; | ||
/// <summary> | ||
/// Number between -2.0 and 2.0. | ||
/// Positive values penalize new tokens based on their existing frequency in the text so far | ||
/// decreasing the model's likelihood to repeat the same line verbatim | ||
/// </summary> | ||
public double frequency_penalty { get; set; } = 0; | ||
/// <summary> | ||
/// The messages to generate chat completions for | ||
/// </summary> | ||
public List<Message> messages { get; set; } = new List<Message>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Newtonsoft.Json.Converters; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChatGPT.API.Framework | ||
{ | ||
public class Message | ||
{ | ||
/// <summary> | ||
/// Role | ||
/// </summary> | ||
[JsonConverter(typeof(StringEnumConverter))] | ||
public RoleType role { get; set; } | ||
public enum RoleType | ||
{ | ||
system, | ||
user, | ||
assistant | ||
} | ||
/// <summary> | ||
/// Content | ||
/// </summary> | ||
public string content; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// 有关程序集的一般信息由以下 | ||
// 控制。更改这些特性值可修改 | ||
// 与程序集关联的信息。 | ||
[assembly: AssemblyTitle("ChatGPT.API.Framework")] | ||
[assembly: AssemblyDescription("")] | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("ChatGPT.API.Framework")] | ||
[assembly: AssemblyCopyright("Copyright © 2023")] | ||
[assembly: AssemblyTrademark("")] | ||
[assembly: AssemblyCulture("")] | ||
|
||
// 将 ComVisible 设置为 false 会使此程序集中的类型 | ||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 | ||
//请将此类型的 ComVisible 特性设置为 true。 | ||
[assembly: ComVisible(false)] | ||
|
||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID | ||
[assembly: Guid("c93b67c8-7f98-452b-b2cb-03d1f54ca244")] | ||
|
||
// 程序集的版本信息由下列四个值组成: | ||
// | ||
// 主版本 | ||
// 次版本 | ||
// 生成号 | ||
// 修订号 | ||
// | ||
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 | ||
//通过使用 "*",如下所示: | ||
// [assembly: AssemblyVersion("1.0.*")] | ||
[assembly: AssemblyVersion("1.0.0.0")] | ||
[assembly: AssemblyFileVersion("1.0.0.0")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChatGPT.API.Framework | ||
{ | ||
public class Response | ||
{ | ||
public string id { get; set; } | ||
[JsonProperty(PropertyName = "object")] | ||
public string _object { get; set; } | ||
public int created { get; set; } | ||
public Usage usage { get; set; } | ||
public Choices[] choices { get; set; } | ||
/// <summary> | ||
/// Usage | ||
/// </summary> | ||
public class Usage | ||
{ | ||
public int prompt_tokens; | ||
public int completion_tokens; | ||
public int total_tokens; | ||
} | ||
|
||
public class Choices | ||
{ | ||
public int index; | ||
public Message message; | ||
public string finish_reason; | ||
} | ||
/// <summary> | ||
/// Get Response Message | ||
/// </summary> | ||
public string GetMessageContent() => choices[0].message.content; | ||
/// <summary> | ||
/// Get Response Message | ||
/// </summary> | ||
public Message GetMessage() => choices[0].message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net462" /> | ||
</packages> |
Oops, something went wrong.