replace
StringContent(json.dumps(content))
by
StringContent(json.dumps(content), Encoding.UTF8, "application/json")
example with Newtonsoft.Json
import sys
import clr
import System
from System.Text import *
from System.Net import *
clr.AddReference("System.Net.Http")
from System.Net.Http import *
clr.AddReference('Newtonsoft.Json')
import Newtonsoft.Json;
clr.AddReference("System.Core")
clr.ImportExtensions(System.Linq)
apikey = "sk-bC.............." # YOUR_API_KEY
def translate_OpenAI(text="maison", source="french", target="english"):
input = "Hello translate this {0} word '{2}' in {1}.Write only the answer (word or expression)".format(source, target, text)
result = None
with HttpClient() as client:
client.DefaultRequestHeaders.Add("Authorization", "Bearer {}".format(apikey))
content = {\
"model": "gpt-3.5-turbo", \
'max_tokens': 100 ,\
"messages": [{"role": "user", "content": input}], \
}
jsonRequest = Newtonsoft.Json.JsonConvert.SerializeObject(content);
httpContent = StringContent(jsonRequest, Encoding.UTF8, "application/json");
response = client.PostAsync("https://api.openai.com/v1/chat/completions", httpContent)
jsonResponse = response.Result.Content.ReadAsStringAsync().Result
#print(jsonResponse)
data = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonResponse);
result = data["choices"][0]["message"]["content"].ToString()
return result
OUT = translate_OpenAI(IN[0])
