Excellent software and practical tutorials
Use .NET to quickly build a ChatGPT server, open OpenAI API
ChatGPT Server Play with everyone on WeChat ChatGPT 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:
Build ChatGPT server
First, create a new webapi service program
.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).
After creation, add the registration of HttpClient service in the program, which will be used to access the OpenAI API.
Create a new controller called RobotController to provide a webapi interface for testing.
In the newly created controller, make some initial preparations, such as injecting IHttpClientFactory for backup.
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.
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.
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.
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.
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.
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.
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
https://pan.baidu.com/s/1aIWQHpgifxri3InYUgHWjQ?pwd=chat
https://pan.baidu.com/s/10XD3n_8gdphH7EJBX45Bbw?pwd=rbot
https://pan.baidu.com/s/1SMb2mW7DcoyCpSfEO2-2-w?pwd=chat