The escape sequence for the OpenAI code translator is not debugging through big codes. small codes like addition, subtraction. conversion and all are working.
I tried to create OpenAI code translator using C#, and got the result in converting python code( for converting miles to kms) - to c# and Java Codes respectively.
Pasting my OpenAI custom code (Translating sum of 2 numbers from Python to c#)
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text.Json;
namespace OpenAIProgram
{
public class CodeTranslator
{
public static async Task<string> TranslateCodeAsync(string apiKey, string apiUrl, string jsonBody)
{
string result = string.Empty;
try
{
if (string.IsNullOrEmpty(apiUrl) || string.IsNullOrEmpty(jsonBody))
throw new Exception("Missing parameter for execution");
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
var request = new HttpRequestMessage(HttpMethod.Post, apiUrl);
request.Headers.Add("Connection", "keep-alive");
request.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
//Console.WriteLine("Payload:");
//Console.WriteLine(jsonBody);
var response = await httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync();
}
else
{
throw new Exception("Response code was " + response.StatusCode);
}
}
catch (Exception e)
{
result = e.Message;
result += "\n" + e.GetType() + ": " + e.Message;
if (e.StackTrace != null)
{
foreach (var trace in e.StackTrace.Split("\n"))
{
result += "\n\t" + trace.Trim();
}
}
}
return result;
}
public static async Task Main()
{
string apiKey = "";
string apiUrl = "https://api.openai.com/v1/chat/completions";
string jsonBody = @"
{
""model"": ""gpt-3.5-turbo"",
""messages"": [
{
""role"": ""system"",
""content"": ""You are a genius level coder, with knowledge of all programming and scripting languages and their associated syntax.""
},
{
""role"": ""system"",
""content"": ""Translate the code from [Python] to [c#]""`your text`
},
{
""role"": ""user"",
""content"": ""num1 = 1.5 num2 = 6.3 sum = num1 + num2 print(The sum of {num1} and {num2} is { sum})""
}
],
""temperature"": 1.0,
""max_tokens"": 150
}";
string translatedCode = await TranslateCodeAsync(apiKey, apiUrl, jsonBody);
//string stringjson = JsonConvert.SerializeObject(jsonBody);
//Console.WriteLine("Translated Code:");
Console.WriteLine(translatedCode);
}
}
}
The issue happens when we input codes having different verbatim literals , and they must be translated from their origin code to the destination code(output) as we need.
The main concern is in an issue , where as in c# and Java we use '#' symbol inside the code(input), but once it gets translated to Python the scenario totally changes as in Python the symbol '#' is using for commenting the code.
Issue:
In the code given above, the user provides the code of sum of 2 numbers and if i have to try to convert kilometers to miles or anyother code which have other literals like @,!,$,%,&.*./,? it gives me the response of BadRequest.
You can try on the same code, as it was working perfectly. The inputting JsonBody seems to be not taken properly inside while debugging. For the literal " we can use " " to escape the sequence.