.NET Tutorial on Building OpenAI ChatGPT Server

ChatGPT server WeChat people are having a lot of fun playing with ChatGPT chatbots; however, as ChatGPT has been banned by wx, using WeChat chatbots in wx may lead to the risk of being banned. If you don't want to use ChatGPT on the OpenAI official website every time, it's better to build a service to call ChatGPT locally. After all, OpenAI's API is open to the public.

Use .NET to quickly build a ChatGPT server, open OpenAI API

Play with everyone on WeChat Chatbots are fun to play with; however, as ChatGPT is banned by wx, using WeChat chatbots in wx may lead to the risk of being blocked. If you don’t want to use ChatGPT on the OpenAI official website every time, it is better to build a service to call ChatGPT locally. After all, OpenAI’s API is open to the public.

The chat dialogue on the official website has limitations. If the reply content is long, it may cause exceptions such as request timeout after a certain period of time. Direct access through the API can avoid this situation.

Below is some official information. You can build a ChatGPT server yourself to provide services to others.

Official website document address:https://beta.openai.com/docs/introduction

If you don’t know how to register an OpenAI account, you can refer to:

Detailed guide to registering ChatGPT on the foreign code receiving platform SMS-Activate, ChatGPT registration is 100% successful!

Build ChatGPT server

First, create a new webapi service program

Use .NET to quickly build a ChatGPT server, open OpenAI API

.NET 6 is used here, and you can choose your favorite environment. For ease of reading, I chose to use controllers and enable OpenAPI support (swagger).

Use .NET to quickly build a ChatGPT server, open OpenAI API

After creation, add the registration of HttpClient service in the program, which will be used to access the OpenAI API.

Use .NET to quickly build a ChatGPT server, open OpenAI API

Create a new controller called RobotController to provide a webapi interface for testing.

Use .NET to quickly build a ChatGPT server, open OpenAI API

In the newly created controller, make some initial preparations, such as injecting IHttpClientFactory for backup.

Use .NET to quickly build a ChatGPT server, open OpenAI API

There is some information on the official website. For example, the maximum number of tokens for the text-davinci-003 model is 4000, so there is a request parameter part at the end, which cannot exceed this number.

Use .NET to quickly build a ChatGPT server, open OpenAI API

 

This is a parameter recommendation. Set temperature to 0.9f and top_p to 1. According to the document, the value of temperature will affect some characteristics of the answer content, such as the proportion of content that may be designed to be unfriendly.

Use .NET to quickly build a ChatGPT server, open OpenAI API

We make a general request entity class to serve as the parameter information required to access the webapi interface we provide. Three pieces of information are more important and can be used as parameters for fine-tuning. For example, the document recommends that temp is 0.9f and max tokens is 4000. We can set it to other values for fine-tuning, etc. The message field is our own request parameter, which is used to communicate with the robot.

Use .NET to quickly build a ChatGPT server, open OpenAI API

Then comes the return body. This format is configured by parsing the return value of openai. You can refer to it at will, or you can return the string directly without parsing. It is a string of Json data anyway, so it is not a big problem.

Use .NET to quickly build a ChatGPT server, open OpenAI API

Then we will improve the Call method. The general content is as follows. Among them, openaiKey is my personal key, so I slightly mosaiced it to avoid privacy leakage. I hope you understand. For other code content, you can directly see the screenshot code.

Use .NET to quickly build a ChatGPT server, open OpenAI API

Finally, start the service program and enter swagger to call the interface. For example, in the message field, I passed "Help me write a C# version of Hello World", and the return value body is the first data in the choices[] array, and text is the content of the robot's reply.

Use .NET to quickly build a ChatGPT server, open OpenAI API

The above is just a simple way of writing. You can expand or modify it according to your needs. For example, you can use the existing keys and rules to write a chat service in other languages, or write a chat client to access it, etc. Everything is possible. Or if WeChat is blocked, you can try to build a service yourself to indirectly continue to implement the intelligent chat service of some APPs, etc.

ChatGPT server download:https://www.mediafire.com/file/zfhax9qcbjzooq3/RobotServer.zip/file

The original link of this blog is:https://www.cnblogs.com/weskynet/p/16987108.html

score

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *