Giter Site home page Giter Site logo

okgodoit / openai-api-dotnet Goto Github PK

View Code? Open in Web Editor NEW
1.7K 63.0 396.0 2.02 MB

An unofficial C#/.NET SDK for accessing the OpenAI GPT-3 API

Home Page: https://www.nuget.org/packages/OpenAI/

License: Other

C# 100.00%
openai gpt-3 gpt3 ml machine-learning sdk csharp dotnet chatgpt dall-e

openai-api-dotnet's Introduction

Roger Pincombe

OkGoDoIt

I am an experienced engineering leader, product creator, and full stack engineer. I have over a decade of broad experience with technologies, industries, and scales of operation, having founded multiple startups in the AI and Ad-Tech industries, worked at enterprises as notable as Microsoft and Salesforce, won over 15 hackathons, and regularly built new products for fun or freelance. My engineering often involves machine learning, web scraping, and integrating many disparate systems. Some of my more interesting personal projects include the OpenAI GPT3 .Net SDK, the AllThePeople.net social media data mining system, and a teleprompter for Google Glass. Additionally, I started and run a major San Francisco live theater venue and am experienced at presenting at conferences and managing events in both professional and entertainment contexts. I am based in San Francisco but value location flexibility.

Recent projects include

Find me online

💻 Check out my blog: rogerpincombe.com

😎 Read more about me

🦾 Download my resume

🔗 Connect with me on LinkedIn

openai-api-dotnet's People

Contributors

babrekel avatar dependabot[bot] avatar dex3r avatar esp0 avatar gotmike avatar hongxumeng avatar jasonwei512 avatar lofcz avatar megalon avatar metjuperry avatar okgodoit avatar pandapknaepel avatar quangsi28 avatar tomaustin700 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

openai-api-dotnet's Issues

Using the OpenAI-API-dotnet library on Azure App Service API

Hi,

I'm trying to use the OpenAI-API-dotnet library in my custom API code that I'm hosting on Azure App Service. When I debug my code locally on my laptop it works and my custom API is able to return sample questions that is generated by the OpenAI Completions endpoint. But when I deployed my code to the Azure App Service and when I test the API from the Azure App Service endpoint, it returns null. Is there anything that I need to do to get the library to work from an Azure App Service deployment ?

Fine Tuning

Is it fine tuning coming avaivable anytime soon in this kit ?

File Upload for Fine Tuning Not Working

I have installed this via nuget and its using v 1.5.0.

I have created a fine tune file with the following line

{"prompt": "test", "completion": "test 123"}

I am uploading it with the following

var response = await api.Files.UploadFileAsync("fine-tune.jsonl");

When the code runs, I get the following error

Error at files (https://api.openai.com/v1/files) with HTTP status code: BadRequest. Content: {
  "error": {
    "message": "Expected file to have JSONL format, where every line is a JSON dictionary. Line 1 is not a dictionary (HINT: line starts with: \"{\"p...\").",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}

Endpoint Properties in IOpenAIAPI interface

I have made an error in the IOpenAIAPI interface (and the OpenAIAPI class). The endpoint properties should be defined as interfaces instead of classes so that they are also suitable for Moq tests. Additionally, the IChatEndpoint interface should be added as a property.

Wrong:

CompletionEndpoint Completions { get; }

Correct:

ICompletionEndpoint Completions { get; }

API Key Validation

How do you validate the key? I'd rather not call API and generate the text and discard it just to validate the key...
Using fake key, for example "1234" produces some weird errors...

Any ideas?

Request to add Fileupload support

Hi, Hope your doing well. I have generated the code to implement file upload support with OpenAI. Would you be interested in merging the code to add FileUpload support to your library? I'd be glad to work with you now that I have a POC fully working.
if your interested
Nicholas {AT] DesertComputerAgents (Dot] !com>

Im sure we can knock it out in a single convo. Youll get a auto response from my email, you can disregard it.

Not very descriptive error messages

I was curious about GPT-3 and I found this project/library so I decided to try it. But when I run the example code snippet I get a NullReferenceException:

image

Not sure what is happening, maybe it needs some API key or something. But it will be nice if the error message was a little more descriptive.

Azure OpenAI issue

I'm trying to use this library with an Azure OpenIA resource.
I'm using the example in the readme: OpenAIAPI api = OpenAIAPI.ForAzure("YourResourceName", "deploymentId", "api-key");, but Url created seems to be wrong.

OpenAIAPI api object initialization:
OpenAIAPI api = OpenAIAPI.ForAzure("opeanai-resource", "deployedEngine", "apikey");
CreatedUrl:
https://opeanai-resource.openai.azure.com/openai/deployments/deployedEngine)/{1}?api-version={0}

It adds a clos parenthesis to the resource engine, so I get this error: The API deployment for the resource does not exist.

I guess the ApiVersion {0} and the completion {1} are added when doing the requests, but are not showed in the Url

[Solved: needed to catch exceptions] Doesn't seem very reliable, in Unity?

Easy to use. Looks great. When it works, it's great :) However, I find it only succeeds about 30% of the time, for completions. The other 70% of the time, there's no return, and no error.

It's not my network or api key, since if I use my own code to call openapi api, I get 100% successful returns.

Example code I'm using for your API:

        OpenAIAPI api = new OpenAIAPI(Auth.openai_api_key);
        CompletionRequest completionRequest = new CompletionRequest{
            Prompt = "Q: Hi, how are you? A: ",
            Model = "text-davinci-003",
            MaxTokens = 256,
            Temperature = 0.7f,
            Echo = true
        };

        if(true) {
            // non-streaming
            CompletionResult result = await api.Completions.CreateCompletionAsync(completionRequest);
            Debug.Log(result.ToString());
        }

        if(false) {
        // streaming
        Debug.Log("calling openapi...");
        await api.Completions.StreamCompletionAsync(
            completionRequest,
            result => {
                try {
                    Debug.Log($"result to string{result.ToString()}");
                } catch(Exception e) {
                    Debug.Log($"exception {e.ToString()}");
                }
        });
        }

How to call chat api to start a continious converation instead of a single isolated q/a

I currently use below code to call the chat api

  var results = await api.Chat.CreateChatCompletionAsync(new ChatRequest()
                  {
                      Model = Model.ChatGPTTurbo,
                      Temperature = 0.1,
                      MaxTokens = max_Tokens,
                      Messages = new ChatMessage[]
                      {
                        new ChatMessage(ChatMessageRole.User, prompt)
                      }
                  });
                  answer = results.Choices[0].Message.Content.Trim();

but how to call chat api to start a continious converation, just like in the chatgpt official web interface, instead of a single isolated q/a like above.

some update: I just found below code to create a conversation:

var api = new OpenAI_API.OpenAIAPI(apiKey);

                       var chat = api.Chat.CreateConversation();
                       chat.AppendUserInput(prompt);

Many thanks, I am new to c#. Any example code is highly appreciated.

Best model for advance machine translation?

HI,

Im looking for best way to use GPT for text translation. Can you please advice me how, or what enpoint use it for that ? I would like to also pass instruction for context and glossary.

thank you for help

Whitespace optional in SSE event format

GPT used the server-side-events format to send streamed completions https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format

Currently the data: prefix is treated as optional, but any line without it should be ignored, and in particular any line starting with just : should be a comment.

Also the space should be optional, both data: and data: should work.

if (line.StartsWith("data: "))
line = line.Substring("data: ".Length);

How to employ "numOutputs" > 1

I see that CompletionRequest can ask for more than one output, but I seem to only be able to retrieve the first one using "GetString()". Any guidance on how to get multiple responses? Thanks!

Setup some code guidelines through editorconfig

Lately we've been doing a lot with ChatGPT, and as this is one of the popular ways to communicate with OpenAI it'd be nice to standardize and enforce the codestyle. This makes the code:

  1. More readable
  2. Easier to work with
  3. Maintainable when multiple people work on it

Microsoft gives a great example of these codeconventions for naming, what to use etc. : https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions

Todo

  • Add editorconfig
  • fix the warnings
  • Remove current Pragmas/ignores and fix the underlying problem
  • Enable code analysis
  • Optional: Treat warnings as errors
  • Introduce CI which fails when warnings are found

Create interfaces for the OpenAIAPI and the Endpoints

With interfaces u can use moq to create (or generate 😄) unit tests.
Also think about to create an config object for the api key, so that it can be added to the di.
And an extension method to add the api client to the di would be nice.
And the naming should be OpenAiApi, or not?

Can we select what engine or model?

The current engine/model seems to be very mundane (dumb i dare say?) unable to answer very basic question,
but sometimes surprises me with a very complete response, very sporadic.

Model-string conversions should not be implicit as they lose information

Model has an implicit conversion to a string using the name of the model:

public static implicit operator string(Model model)
{
return model?.ModelID;
}
/// <summary>
/// Allows a string to be implicitly cast as an <see cref="Model"/> with that <see cref="ModelID"/>
/// </summary>
/// <param name="name">The id/<see cref="ModelID"/> to use</param>
public static implicit operator Model(string name)
{
return new Model(name);
}

This loses information, as the model also contains additional information, so:

var m1 = new Model("text-davinci-003") { OwnedBy = "openai" };
string s = m1;
Model m2 = s;

bool lost = m2.OwnedBy != m1.OwnedBy;

Conversions that lose information (for instance double to int) should use explicit conversions.

Cant install

I get these error when running Install-Package OpenAI:

Install-Package : NU1108: Cycle detected.
OpenAI -> OpenAI (>= 1.6.0).
At line:1 char:1

  • Install-Package OpenAI
  •   + CategoryInfo          : NotSpecified: (:) [Install-Package], Exception
      + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
    
    

Install-Package : Package restore failed. Rolling back package changes for 'OpenAI'.
At line:1 char:1

  • Install-Package OpenAI
  •   + CategoryInfo          : NotSpecified: (:) [Install-Package], Exception
      + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
    

Beta Instruct

I see there is already a pull request to add the beta-instruct engines. Is this ready to commit/implement? I am looking to use that functionality.

Logprobs results in Newtonsoft SerializationException

Adding logProbs: n, to a CompletionRequest object, results in a serialization error "One or more errors occurred. (Error converting value {null} to type 'System.Double'. Path 'choices[0].logprobs.token_logprobs[0]', line 1, position 418.)"

I have tried using logProbs with both CreateCompletionAsync and CreateCompletionsAsync and I recieve the same error.
I wonder whether the JSON being returned from OpenAI has changed slightly?

latest release showing v1.2 instead of v1.3 on github

i notice that the latest release on github is v1.2, but i have seen in issues/discussions that we are talking about v1.3.

do we need to have a new release?

also: i have been idle for a while, happy to jump in and help wrap up some of these issues. please bear with me.

URL difference?

Maybe I do not understand something, but I look at the library API and its URLSs, and I also tried them and they work:
$"https://api.openai.com/v1/engines/{Api.EngineName}/completions"
where {Api.EngineName} is the model name.

and I also look at the OpenAI documentation (below) and they are different...
Can you explain?

https://api.openai.com/v1/completions

or in curl:
curl https://api.openai.com/v1/completions
-H 'Content-Type: application/json'
-H 'Authorization: Bearer YOUR_API_KEY'
-d '{
"model": "text-davinci-003",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0
}'

Setup GH Actions CI/CD

Each program/library in 2023 should have some form of automation to ensure stuff builds and can be deployed without manually opening up VS(code) and build the project.

GH offers a feature called Github Actions which does exactly that. Automate the stuff like builds and create releases.

This ensures quality, and can execute your tests automaticly.

Todo

  • Add a GH Workflow
    • Build the library
      • Static analysis
    • Test the library
    • Deploy the library
    • Create release notes
  • Think of a versioning system, preferably semver

Some place to discuss/talk

Currently there's no place to discuss certain things except for the issue tracker.
Personally I love to use Discord for this as opening a lot of issues as questions just litters the project in my honest opinion.
Also the discussion tab from Github is a place which is missed a lot so therefore creating some place of realtime messaging is handy.

Let me know your thoughts and if you need help with it 👍

logit_bias

请问作者考虑支持logit_bias参数么?在一些屏蔽关键词的场合还是挺有用的

Downgrading to .NET Framework 4.x

For legacy reasons we wanted to use your project against a .Net Framework 4.x project but we cannot get it to compile.
Errors with your use of the latest v8 C# foreach construct.
When we use your library compiled to .NET Standard 2.0, we get the following error from .NET Framework 4.x projects:
System.Net.Http.HttpRequestException: 'An error occurred while sending the request.

Any pointers would be helpful?

Completions, in my case, is not working..

My use case is based on an example, repeated on a new text. It's working by CURL, but not using the method:
var results = api.Completions.CreateCompletionsAsync(new CompletionRequest(body , temperature: 0, max_tokens: 250, top_p: 1), 1).Result;

this is the body:
get some information from this text on a list.

[Text1]lorem ipsum...

[List1]
name: John
Last: Doe
Age: 100
Date of birth: 1900/01/01

[Text2] {new text added dynamicaly}

[List2]
name:

and that's what I am waiting.. the competition of that List2.
It's working on the playground, it's working by CURL.
It's not working using your method..
The response is the same text as List1:
name: John Last: Doe Age: 100 Date of birth: 1900/01/01
Any suggestion?
Welcome to work with you to solve this.

Thanks!

naming ambiguity

some names are already taken:

File is a class in System.IO namespace.

source generators often use file as keyword denoting type modifier

file class X
{
// implementation
}

Use HttpClientFactory

Currently you create an HttpClient whenever you need it without a PooledConnectionLifetime:

HttpClient client = new HttpClient();

This means a client per request that opens an HTTP connection but doesn't close it until the GC finalises it.

As per the guidelines either:

  • Use a static or singleton HttpClient instance with PooledConnectionLifetime set to the desired interval, such as two minutes, depending on expected DNS changes. This solves both the port exhaustion and DNS changes problems without adding the overhead of IHttpClientFactory. If you need to be able to mock your handler, you can register it separately.

  • Using IHttpClientFactory, you can have multiple, differently configured clients for different use cases. However, be aware that the factory-created clients are intended to be short-lived, and once the client is created, the factory no longer has control over it.
    The factory pools HttpMessageHandler instances, and, if its lifetime hasn't expired, a handler can be reused from the pool when the factory creates a new HttpClient instance. This reuse avoids any socket exhaustion issues.
    If you desire the configurability that IHttpClientFactory provides, we recommend using the typed-client approach.

HttpStreamingRequest reads the headers again for each loop

In a while ((line = await reader.ReadLineAsync()) != null) loop the response headers are read each time:

try
{
res.Organization = response.Headers.GetValues("Openai-Organization").FirstOrDefault();
res.RequestId = response.Headers.GetValues("X-Request-ID").FirstOrDefault();
res.ProcessingTime = TimeSpan.FromMilliseconds(int.Parse(response.Headers.GetValues("Openai-Processing-Ms").First()));
res.OpenaiVersion = response.Headers.GetValues("Openai-Version").FirstOrDefault();
if (string.IsNullOrEmpty(res.Model))
res.Model = response.Headers.GetValues("Openai-Model").FirstOrDefault();
}
catch (Exception e)
{
Debug.Print($"Issue parsing metadata of OpenAi Response. Url: {url}, Error: {e.ToString()}, Response: {resultAsString}. This is probably ignorable.");
}

These don't change and are in a try-catch that is expensive when a throw happens.

Is the playground system available?

Does that exist? Is there like an engine that uses the playground system or does whatever you want (text-wise)

e.g. The request is "Write a random essay for me".

I'm new to OpenAI, so I'm not sure what it's called and if you added it or not.

Q&A Request

I can't see out how to make a script to use the Q&A portion.. can you help me?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.