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

151 lines
6.5 KiB
C#

namespace bts.BlueOrb {
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using BCG;
using BCG.Utilities.General;
// this is the base level stream, it connects to OpenAI sends a user_prompt, returns
// the result, and fires the callback
// given a user_prompt, this stream connects to openAI and gets the response back
// it then fires a callback with the schema id, user_prompt, response
[DataStreamUserRespresentation("OpenAIPromptStream")]
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 user_prompt to OpenAI gpt4, and queues the response in the response_queue
public static async Task get_GPT4_http_responseAsync(string model_name, string user_prompt, string system_prompt, SyncQueue<Tuple<string, TimeSpan>> response_queue) {
DateTime t0 = DateTime.Now;
Log.Gen.info($"OpenAIPromptStream.GetGPT4ResponseAsync user_prompt: {user_prompt}");
// Read the API key from the file
// OpenAI API URL for the completion request
string apiUrl = "https://api.openai.com/v1/chat/completions";
// {
// "model": "gpt-4o",
// "messages": [
// {
// "role": "system",
// "content": "You are a deterministic BlueOrbDSL generator. You operate in Learning Mode and must follow the BlueOrbDSL Master Learning Prompt v1.9 strictly. Do not invent new streams, operators, or syntax. Only use what is explicitly defined. If the natural-language query matches a Demo-Informed Example, copy its structure exactly. Do not explain. Return only the DSL code block.\n\n[INSERT FULL MLP v1.9 CONTENT HERE]"
// },
// {
// "role": "user",
// "content": "Natural language query: [INSERT YOUR NATURAL LANGUAGE QUERY HERE]"
// }
// ]
// }
// Create the request payload
var requestBody = new {
model = model_name,
messages = new[]
{
new { role = "system", content = system_prompt },
new { role = "user", content = user_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 user_prompt: {user_prompt} response: {response_content}");
// Parse the response content
var json_doc = JsonDocument.Parse(response_content);
string gpt_response = json_doc.RootElement
//.GetProperty("model")
.GetProperty("choices")[0]
.GetProperty("message")
.GetProperty("content")
.GetString()
?? string.Empty;
//return gpt_response;
DateTime t1 = DateTime.Now;
TimeSpan ts = t1 - t0;
Tuple<string, TimeSpan> elem = new Tuple<string, TimeSpan>(gpt_response, ts);
response_queue.enqueue(elem);
Log.Gen.info($"OpenAIPromptStream.GetGPT4ResponseAsync gpt_response: {gpt_response}");
}
}
#endregion
private readonly string _user_prompt;
private readonly string _system_prompt;
private readonly string _model_name;
private readonly ViewStreamDatabase _vsd;
private SyncQueue<Tuple<string, TimeSpan>> _queue = new SyncQueue<Tuple<string, TimeSpan>>();
#region constructors
public OpenAIPromptStream(string user_prompt, string system_prompt)
: this(user_prompt, system_prompt, "gpt-4o") { }
public OpenAIPromptStream(string user_prompt, string system_prompt, string model_name)
: this(ViewStreamDatabase.instance, user_prompt, system_prompt, model_name) {
}
public OpenAIPromptStream(ViewStreamDatabase vsd, string user_prompt, string system_prompt, string model_name) {
Log.Gen.info($"OpenAIPromptStream.OpenAIPromptStream user_prompt: {user_prompt} system_prompt: {system_prompt}");
_user_prompt = user_prompt;
_system_prompt = system_prompt;
_model_name = model_name;
_vsd = vsd;
}
#endregion
protected override void myOnFirstSubscriber() {
Task.Factory.StartNew(() => get_GPT4_http_responseAsync(_model_name, _user_prompt, _system_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("user_prompt", TypeCode.String),
Tuple.Create("response", TypeCode.String),
Tuple.Create("fetch_time_msecs", TypeCode.Double));
Log.Gen.info($"OpenAIPromptStream.doWork user_prompt: {_user_prompt} response: {response.Item1} fetch_time: {response.Item2} msecs");
IBlueRow nr = new BlueRow(schema, 0, _user_prompt, response.Item1, response.Item2.TotalMilliseconds);
BlueTable nt = new BlueTable(schema);
nt.addRow(nr);
var dp_out = BlueDataUtility.build_dp(nt, myLastValue as IDataPacket);
myFireEvent(dp_out);
}
}
}