使用 .NET MAUI 开发 ChatGPT 客户端的流程(使用内在价值法对股票进行估值的方法)奔走相告

随心笔谈2年前发布 编辑
156 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

 public static async Task<CompletionsResponse> GetResponseDataAsync(string prompt)
{
// Set up the API URL and API key
string apiUrl=”https://api.openai.com/v1/completions”;

// Get the request body JSON
decimal temperature=decimal.Parse(Setting.Temperature, CultureInfo.InvariantCulture);
int maxTokens=int.Parse(Setting.MaxTokens, CultureInfo.InvariantCulture);
string requestBodyJson=GetRequestBodyJson(prompt, temperature, maxTokens);

// Send the API request and get the response data
return await SendApiRequestAsync(apiUrl, Setting.ApiKey, requestBodyJson);
}

private static string GetRequestBodyJson(string prompt, decimal temperature, int maxTokens)
{
// Set up the request body
var requestBody=new CompletionsRequestBody
{
Model=”text-davinci-003″,
Prompt=prompt,
Temperature=temperature,
MaxTokens=maxTokens,
TopP=1.0m,
FrequencyPenalty=0.0m,
PresencePenalty=0.0m,
N=1,
Stop=”[END]”,
};

// Create a new JsonSerializerOptions object with the IgnoreNullValues and IgnoreReadOnlyProperties properties set to true
var serializerOptions=new JsonSerializerOptions
{
IgnoreNullValues=true,
IgnoreReadOnlyProperties=true,
};

// Serialize the request body to JSON using the JsonSerializer.Serialize method overload that takes a JsonSerializerOptions parameter
return JsonSerializer.Serialize(requestBody, serializerOptions);
}

private static async Task<CompletionsResponse> SendApiRequestAsync(string apiUrl, string apiKey, string requestBodyJson)
{
// Create a new HttpClient for making the API request
using HttpClient client=new HttpClient();

// Set the API key in the request headers
client.DefaultRequestHeaders.Add(“Authorization”, “Bearer ” + apiKey);

// Create a new StringContent object with the JSON payload and the correct content type
StringContent content=new StringContent(requestBodyJson, Encoding.UTF8, “application/json”);

// Send the API request and get the response
HttpResponseMessage response=await client.PostAsync(apiUrl, content);

// Deserialize the response
var responseBody=await response.Content.ReadAsStringAsync();

// Return the response data
return JsonSerializer.Deserialize<CompletionsResponse>(responseBody);
}

© 版权声明

相关文章