Files
spark/src/DataStreamSuite/SharedStreams/NL/OpenAIPromptStream.cs
T

114 lines
4.7 KiB
C#

namespace bts.SilverPine {
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
// using OpenAI.GPT3;
// using OpenAI.GPT3.ObjectModels.RequestModels;
// using OpenAI.GPT3.Managers;
using BCG;
using BCG.Utilities.General;
// given a prompt, this stream connects to openAI and gets the response back
// it then fires a callback with the schema id, prompt, response
public class OpenAIPromptStream : ViewStream, IWorker {
#region OpenAI communication
static string api_key = //File.ReadAllText("OpenAIGPT4.key");
"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";
// Uses HttpClient to send the prompt to OpenAI gpt4, and queues the response in the response_queue
public static async Task get_GPT4_http_responseAsync(string prompt, SyncQueue<string> response_queue) {
Log.Gen.info($"OpenAIPromptStream.GetGPT4ResponseAsync prompt: {prompt}");
// Read the API key from the file
// OpenAI API URL for the completion request
string apiUrl = "https://api.openai.com/v1/chat/completions";
// Create the request payload
var requestBody = new {
model = "gpt-3.5-turbo",
messages = new[]
{
new { role = "user", content = prompt }
},
max_tokens = 500 // Adjust based on your response length needs
};
// Serialize the payload to JSON
string jsonRequestBody = JsonSerializer.Serialize(requestBody);
using (HttpClient client = new HttpClient()) {
// Add the OpenAI API key to the Authorization header
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {api_key}");
// Prepare the request content
HttpContent content = new StringContent(jsonRequestBody, Encoding.UTF8, "application/json");
// Send the POST request to OpenAI API
HttpResponseMessage response = await client.PostAsync(apiUrl, content);
// Ensure the response is successful
response.EnsureSuccessStatusCode();
// Read the response content as a string
string response_content = await response.Content.ReadAsStringAsync();
Log.Gen.info($"OpenAIPromptStream.GetGPT4ResponseAsync prompt: {prompt} response: {response_content}");
// Parse the response content
var json_doc = JsonDocument.Parse(response_content);
string gpt_response = json_doc.RootElement
.GetProperty("choices")[0]
.GetProperty("message")
.GetProperty("content")
.GetString();
//return gpt_response;
response_queue.enqueue(gpt_response);
Log.Gen.info($"OpenAIPromptStream.GetGPT4ResponseAsync gpt_response: {gpt_response}");
}
}
#endregion
private readonly string _prompt;
private readonly ViewStreamDatabase _vsd;
private SyncQueue<string> _queue = new SyncQueue<string>();
public OpenAIPromptStream(string prompt)
: this(ViewStreamDatabase.instance, prompt) {
}
public OpenAIPromptStream(ViewStreamDatabase vsd, string prompt) {
Log.Gen.info($"OpenAIPromptStream.OpenAIPromptStream prompt: {prompt}");
_prompt = prompt;
_vsd = vsd;
}
protected override void myOnFirstSubscriber() {
Task.Factory.StartNew(() => get_GPT4_http_responseAsync(_prompt, _queue));
_vsd.addWorker(this);
}
protected override void myOnLastSubscriber() {
}
public void doWork() {
var response = _queue.dequeue(0);
if (response == null)
return;
IBlueTableSchema schema = BlueDataUtility.form_schema("OpenAIRespone",
Tuple.Create("id", TypeCode.Int32),
Tuple.Create("prompt", TypeCode.String),
Tuple.Create("response", TypeCode.String));
IBlueRow nr = new BlueRow(schema, 0, _prompt, response);
BlueTable nt = new BlueTable(schema);
nt.addRow(nr);
var dp_out = BlueDataUtility.build_dp(nt, myLastValue as DataPacket);
myFireEvent(dp_out);
}
}
}