Guest User

C# OpenAI CURL Test

a guest
Feb 13th, 2024
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.03 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Dynamic;
  4. using System.Net.Http;
  5. using System.Net.Http.Headers;
  6. using System.Net.Http.Json;
  7. using System.Reflection;
  8. using System.Security.Policy;
  9. using System.Text;
  10. using System.Text.Json.Serialization;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using System.Xml;
  14. using System.Xml.Linq;
  15.  
  16. using Tiktoken;
  17.  
  18. using Newtonsoft.Json;
  19. using Newtonsoft.Json.Linq;
  20.  
  21. namespace OpenAICURL
  22. {
  23. class Program
  24. {
  25. static dynamic jsonData = new ExpandoObject();
  26. static string openAIKey = "<REDACTED>";
  27. static string url = "https://api.openai.com/v1/chat/completions";
  28.  
  29. static void SetupJson()
  30. {
  31. jsonData.model = "gpt-3.5-turbo-0125";
  32.  
  33. jsonData.messages = new List<dynamic>();
  34. jsonData.messages.Add(new ExpandoObject());
  35. jsonData.messages[0].role = "system";
  36. jsonData.messages[0].content = "RemoteProcedureCall(GetFunctionIndex) will get a list of all available functions in your host program";
  37.  
  38. jsonData.messages.Add(new ExpandoObject());
  39. jsonData.messages[1].role = "user";
  40. // jsonData.messages[1].content = "Can you tell what functions you have access to?";
  41. jsonData.messages[1].content = "Test the RPC functionality by calling TestFunction(1, 2, 3, 4, 5, 6, 7, 8)";
  42. // jsonData.messages[1].content = "Can you tell me if you have a function available to calculate the Levenshtein distance between 'kitten' and 'sitting'?";
  43. // jsonData.messages[1].content = "Can you calculate the Levenshtein distance between 'kitten' and 'sitting'?";
  44.  
  45. // int cnt1 = CountTokens(jsonData.messages[0].content); // 19
  46. // int cnt2 = CountTokens(jsonData.messages[1].content); // 10
  47.  
  48. jsonData.logprobs = false; // true // false
  49. jsonData.top_logprobs = null; // 5 / null
  50.  
  51. jsonData.tools = new List<dynamic>();
  52. jsonData.tools.Add(new ExpandoObject());
  53. jsonData.tools[0].type = "function";
  54. jsonData.tools[0].function = new ExpandoObject();
  55. jsonData.tools[0].function.name = "RemoteProcedureCall";
  56. jsonData.tools[0].function.description = "Invoke a function (by name) in the program hosting the large language model";
  57.  
  58. jsonData.tools[0].function.parameters = new ExpandoObject();
  59. jsonData.tools[0].function.parameters.type = "object";
  60. jsonData.tools[0].function.parameters.properties = new ExpandoObject();
  61. jsonData.tools[0].function.parameters.properties.functionname = new ExpandoObject();
  62. jsonData.tools[0].function.parameters.properties.functionname.type = "string";
  63. jsonData.tools[0].function.parameters.properties.functionname.description = "Name of function to invoke";
  64.  
  65. {
  66. jsonData.tools[0].function.parameters.properties.parameter1 = new ExpandoObject();
  67. jsonData.tools[0].function.parameters.properties.parameter1.type = "string";
  68. jsonData.tools[0].function.parameters.properties.parameter1.description = "parameter 1";
  69. jsonData.tools[0].function.parameters.properties.parameter2 = new ExpandoObject();
  70. jsonData.tools[0].function.parameters.properties.parameter2.type = "string";
  71. jsonData.tools[0].function.parameters.properties.parameter2.description = "parameter 2";
  72. jsonData.tools[0].function.parameters.properties.parameter3 = new ExpandoObject();
  73. jsonData.tools[0].function.parameters.properties.parameter3.type = "string";
  74. jsonData.tools[0].function.parameters.properties.parameter3.description = "parameter 3";
  75. jsonData.tools[0].function.parameters.properties.parameter4 = new ExpandoObject();
  76. jsonData.tools[0].function.parameters.properties.parameter4.type = "string";
  77. jsonData.tools[0].function.parameters.properties.parameter4.description = "parameter 4";
  78. jsonData.tools[0].function.parameters.properties.parameter5 = new ExpandoObject();
  79. jsonData.tools[0].function.parameters.properties.parameter5.type = "string";
  80. jsonData.tools[0].function.parameters.properties.parameter5.description = "parameter 5";
  81. jsonData.tools[0].function.parameters.properties.parameter6 = new ExpandoObject();
  82. jsonData.tools[0].function.parameters.properties.parameter6.type = "string";
  83. jsonData.tools[0].function.parameters.properties.parameter6.description = "parameter 6";
  84. jsonData.tools[0].function.parameters.properties.parameter7 = new ExpandoObject();
  85. jsonData.tools[0].function.parameters.properties.parameter7.type = "string";
  86. jsonData.tools[0].function.parameters.properties.parameter7.description = "parameter 7";
  87. jsonData.tools[0].function.parameters.properties.parameter8 = new ExpandoObject();
  88. jsonData.tools[0].function.parameters.properties.parameter8.type = "string";
  89. jsonData.tools[0].function.parameters.properties.parameter8.description = "parameter 8";
  90. }
  91.  
  92. jsonData.tools[0].function.parameters.required = new List<string> { "functionname" };
  93.  
  94. jsonData.tool_choice = "auto";
  95. }
  96.  
  97. static async Task Main()
  98. {
  99. // List<string> lines = File.ReadAllLines("cl100k_base.tiktoken.txt").ToList();
  100.  
  101. SetupJson();
  102.  
  103. string responseText = await GetCURLResponse();
  104.  
  105. if (responseText == "")
  106. {
  107. // failure
  108. Debugger.Break();
  109. }
  110. else
  111. {
  112. dynamic obj = JsonConvert.DeserializeObject<dynamic>(responseText);
  113.  
  114. int ptokens = obj.usage.prompt_tokens;
  115. int ctokens = obj.usage.completion_tokens;
  116. dynamic msgobj = obj.choices[0];
  117.  
  118. string msgrole = msgobj.message.role;
  119. string msgcontent = msgobj.message.content;
  120. //dynamic logprobs = msgobj.logprobs;
  121.  
  122. string finishreason = msgobj.finish_reason;
  123.  
  124. if (finishreason == "stop")
  125. {
  126.  
  127. }
  128. else if (finishreason == "length")
  129. {
  130.  
  131. }
  132. else if (finishreason == "tool_calls")
  133. {
  134. int functionCallCount = msgobj.message.tool_calls.Count;
  135.  
  136. for (int n = 0; n < functionCallCount; n++)
  137. {
  138. dynamic tool_calls_data = msgobj.message.tool_calls[n];
  139.  
  140. jsonData.messages.Add(new ExpandoObject());
  141. int index = jsonData.messages.Count - 1;
  142. jsonData.messages[index].role = "tool";
  143. jsonData.messages[index].tool_call_id = tool_calls_data.id;
  144. jsonData.messages[index].name = tool_calls_data.function.name;
  145.  
  146. dynamic argsraw = tool_calls_data.function.arguments;
  147. dynamic args = JsonConvert.DeserializeObject<dynamic>(argsraw.Value);
  148.  
  149. string fname = args.functionname.Value;
  150. string p1 = args.parameter1.Value;
  151. string p2 = args.parameter2.Value;
  152. string p3 = args.parameter3.Value;
  153. string p4 = args.parameter4.Value;
  154. string p5 = args.parameter5.Value;
  155. string p6 = args.parameter6.Value;
  156. string p7 = args.parameter7.Value;
  157. string p8 = args.parameter8.Value; // these could be null
  158.  
  159. string function_response = "TestFunction() successful";
  160. jsonData.messages[index].content = function_response;
  161.  
  162. // remove 'tools' in second call
  163. var expandoDict = (IDictionary<string, object>)jsonData;
  164. expandoDict.Remove("logprobs");
  165. expandoDict.Remove("top_logprobs");
  166. expandoDict.Remove("tools");
  167. expandoDict.Remove("tool_choice");
  168.  
  169. string responseText2 = await GetCURLResponse();
  170. dynamic obj2 = JsonConvert.DeserializeObject<dynamic>(responseText2);
  171.  
  172. Debugger.Break();
  173. }
  174. }
  175. }
  176.  
  177. Debugger.Break();
  178. return;
  179. }
  180.  
  181. public static async Task<string> GetCURLResponse()
  182. {
  183. string jsonString = JsonConvert.SerializeObject(jsonData, Newtonsoft.Json.Formatting.Indented);
  184. Debug.WriteLine(jsonString);
  185.  
  186. string responseText = "";
  187.  
  188. using (var httpClient = new HttpClient())
  189. {
  190. // Set up the HTTP headers
  191. httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // ACCEPT header
  192. httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", openAIKey); // Authorization header
  193.  
  194. // Prepare the content to send
  195. using (var content = new StringContent(jsonString, System.Text.Encoding.UTF8, "application/json"))
  196. {
  197. try
  198. {
  199. // Make the POST request
  200. HttpResponseMessage response = await httpClient.PostAsync(url, content);
  201. response.EnsureSuccessStatusCode();
  202.  
  203. // Read the response as a string
  204. responseText = await response.Content.ReadAsStringAsync();
  205.  
  206. // Output the result
  207. Debug.WriteLine(responseText);
  208. }
  209. catch (HttpRequestException e)
  210. {
  211. // Handle any errors
  212. Debug.WriteLine($"Error making request: {e.Message}");
  213. responseText = "";
  214. }
  215. }
  216.  
  217. return responseText;
  218. }
  219. }
  220.  
  221. public static double LinearProb(double logprob)
  222. {
  223. double percentage = Math.Round(Math.Exp(logprob) * 100, 2);
  224. return percentage;
  225. }
  226.  
  227. public static int CountTokens(string text)
  228. {
  229. var encoding = Tiktoken.Encoding.ForModel("gpt-4");
  230. // var tokens = encoding.Encode("hello world"); // [15339, 1917]
  231. //var text = encoding.Decode(tokens); // hello world
  232. var numberOfTokens = encoding.CountTokens(text); // 2
  233. //var stringTokens = encoding.Explore(text); // ["hello", " world"]
  234.  
  235. return numberOfTokens;
  236. }
  237. }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment