Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Dynamic;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Net.Http.Json;
- using System.Reflection;
- using System.Security.Policy;
- using System.Text;
- using System.Text.Json.Serialization;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Xml;
- using System.Xml.Linq;
- using Tiktoken;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- namespace OpenAICURL
- {
- class Program
- {
- static dynamic jsonData = new ExpandoObject();
- static string openAIKey = "<REDACTED>";
- static string url = "https://api.openai.com/v1/chat/completions";
- static void SetupJson()
- {
- jsonData.model = "gpt-3.5-turbo-0125";
- jsonData.messages = new List<dynamic>();
- jsonData.messages.Add(new ExpandoObject());
- jsonData.messages[0].role = "system";
- jsonData.messages[0].content = "RemoteProcedureCall(GetFunctionIndex) will get a list of all available functions in your host program";
- jsonData.messages.Add(new ExpandoObject());
- jsonData.messages[1].role = "user";
- // jsonData.messages[1].content = "Can you tell what functions you have access to?";
- jsonData.messages[1].content = "Test the RPC functionality by calling TestFunction(1, 2, 3, 4, 5, 6, 7, 8)";
- // jsonData.messages[1].content = "Can you tell me if you have a function available to calculate the Levenshtein distance between 'kitten' and 'sitting'?";
- // jsonData.messages[1].content = "Can you calculate the Levenshtein distance between 'kitten' and 'sitting'?";
- // int cnt1 = CountTokens(jsonData.messages[0].content); // 19
- // int cnt2 = CountTokens(jsonData.messages[1].content); // 10
- jsonData.logprobs = false; // true // false
- jsonData.top_logprobs = null; // 5 / null
- jsonData.tools = new List<dynamic>();
- jsonData.tools.Add(new ExpandoObject());
- jsonData.tools[0].type = "function";
- jsonData.tools[0].function = new ExpandoObject();
- jsonData.tools[0].function.name = "RemoteProcedureCall";
- jsonData.tools[0].function.description = "Invoke a function (by name) in the program hosting the large language model";
- jsonData.tools[0].function.parameters = new ExpandoObject();
- jsonData.tools[0].function.parameters.type = "object";
- jsonData.tools[0].function.parameters.properties = new ExpandoObject();
- jsonData.tools[0].function.parameters.properties.functionname = new ExpandoObject();
- jsonData.tools[0].function.parameters.properties.functionname.type = "string";
- jsonData.tools[0].function.parameters.properties.functionname.description = "Name of function to invoke";
- {
- jsonData.tools[0].function.parameters.properties.parameter1 = new ExpandoObject();
- jsonData.tools[0].function.parameters.properties.parameter1.type = "string";
- jsonData.tools[0].function.parameters.properties.parameter1.description = "parameter 1";
- jsonData.tools[0].function.parameters.properties.parameter2 = new ExpandoObject();
- jsonData.tools[0].function.parameters.properties.parameter2.type = "string";
- jsonData.tools[0].function.parameters.properties.parameter2.description = "parameter 2";
- jsonData.tools[0].function.parameters.properties.parameter3 = new ExpandoObject();
- jsonData.tools[0].function.parameters.properties.parameter3.type = "string";
- jsonData.tools[0].function.parameters.properties.parameter3.description = "parameter 3";
- jsonData.tools[0].function.parameters.properties.parameter4 = new ExpandoObject();
- jsonData.tools[0].function.parameters.properties.parameter4.type = "string";
- jsonData.tools[0].function.parameters.properties.parameter4.description = "parameter 4";
- jsonData.tools[0].function.parameters.properties.parameter5 = new ExpandoObject();
- jsonData.tools[0].function.parameters.properties.parameter5.type = "string";
- jsonData.tools[0].function.parameters.properties.parameter5.description = "parameter 5";
- jsonData.tools[0].function.parameters.properties.parameter6 = new ExpandoObject();
- jsonData.tools[0].function.parameters.properties.parameter6.type = "string";
- jsonData.tools[0].function.parameters.properties.parameter6.description = "parameter 6";
- jsonData.tools[0].function.parameters.properties.parameter7 = new ExpandoObject();
- jsonData.tools[0].function.parameters.properties.parameter7.type = "string";
- jsonData.tools[0].function.parameters.properties.parameter7.description = "parameter 7";
- jsonData.tools[0].function.parameters.properties.parameter8 = new ExpandoObject();
- jsonData.tools[0].function.parameters.properties.parameter8.type = "string";
- jsonData.tools[0].function.parameters.properties.parameter8.description = "parameter 8";
- }
- jsonData.tools[0].function.parameters.required = new List<string> { "functionname" };
- jsonData.tool_choice = "auto";
- }
- static async Task Main()
- {
- // List<string> lines = File.ReadAllLines("cl100k_base.tiktoken.txt").ToList();
- SetupJson();
- string responseText = await GetCURLResponse();
- if (responseText == "")
- {
- // failure
- Debugger.Break();
- }
- else
- {
- dynamic obj = JsonConvert.DeserializeObject<dynamic>(responseText);
- int ptokens = obj.usage.prompt_tokens;
- int ctokens = obj.usage.completion_tokens;
- dynamic msgobj = obj.choices[0];
- string msgrole = msgobj.message.role;
- string msgcontent = msgobj.message.content;
- //dynamic logprobs = msgobj.logprobs;
- string finishreason = msgobj.finish_reason;
- if (finishreason == "stop")
- {
- }
- else if (finishreason == "length")
- {
- }
- else if (finishreason == "tool_calls")
- {
- int functionCallCount = msgobj.message.tool_calls.Count;
- for (int n = 0; n < functionCallCount; n++)
- {
- dynamic tool_calls_data = msgobj.message.tool_calls[n];
- jsonData.messages.Add(new ExpandoObject());
- int index = jsonData.messages.Count - 1;
- jsonData.messages[index].role = "tool";
- jsonData.messages[index].tool_call_id = tool_calls_data.id;
- jsonData.messages[index].name = tool_calls_data.function.name;
- dynamic argsraw = tool_calls_data.function.arguments;
- dynamic args = JsonConvert.DeserializeObject<dynamic>(argsraw.Value);
- string fname = args.functionname.Value;
- string p1 = args.parameter1.Value;
- string p2 = args.parameter2.Value;
- string p3 = args.parameter3.Value;
- string p4 = args.parameter4.Value;
- string p5 = args.parameter5.Value;
- string p6 = args.parameter6.Value;
- string p7 = args.parameter7.Value;
- string p8 = args.parameter8.Value; // these could be null
- string function_response = "TestFunction() successful";
- jsonData.messages[index].content = function_response;
- // remove 'tools' in second call
- var expandoDict = (IDictionary<string, object>)jsonData;
- expandoDict.Remove("logprobs");
- expandoDict.Remove("top_logprobs");
- expandoDict.Remove("tools");
- expandoDict.Remove("tool_choice");
- string responseText2 = await GetCURLResponse();
- dynamic obj2 = JsonConvert.DeserializeObject<dynamic>(responseText2);
- Debugger.Break();
- }
- }
- }
- Debugger.Break();
- return;
- }
- public static async Task<string> GetCURLResponse()
- {
- string jsonString = JsonConvert.SerializeObject(jsonData, Newtonsoft.Json.Formatting.Indented);
- Debug.WriteLine(jsonString);
- string responseText = "";
- using (var httpClient = new HttpClient())
- {
- // Set up the HTTP headers
- httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // ACCEPT header
- httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", openAIKey); // Authorization header
- // Prepare the content to send
- using (var content = new StringContent(jsonString, System.Text.Encoding.UTF8, "application/json"))
- {
- try
- {
- // Make the POST request
- HttpResponseMessage response = await httpClient.PostAsync(url, content);
- response.EnsureSuccessStatusCode();
- // Read the response as a string
- responseText = await response.Content.ReadAsStringAsync();
- // Output the result
- Debug.WriteLine(responseText);
- }
- catch (HttpRequestException e)
- {
- // Handle any errors
- Debug.WriteLine($"Error making request: {e.Message}");
- responseText = "";
- }
- }
- return responseText;
- }
- }
- public static double LinearProb(double logprob)
- {
- double percentage = Math.Round(Math.Exp(logprob) * 100, 2);
- return percentage;
- }
- public static int CountTokens(string text)
- {
- var encoding = Tiktoken.Encoding.ForModel("gpt-4");
- // var tokens = encoding.Encode("hello world"); // [15339, 1917]
- //var text = encoding.Decode(tokens); // hello world
- var numberOfTokens = encoding.CountTokens(text); // 2
- //var stringTokens = encoding.Explore(text); // ["hello", " world"]
- return numberOfTokens;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment