My program accesses the local Nitro server (API Jan for Windows – https://jan.ai/api-reference#tag/chat/post/chat/completions). I’m trying to write a POST request for the “Create chat completion” API request. This is communication with the AI model. Everything works fine in Python and I get the answer. On Delphi 11.3 I get some strange blocks instead of response. But they contain the necessary data (each block contains 1 word). Although I have to receive the JSON and highlight the response in a “content” object. Here is my code.
procedure TForm2.Button1Click(Sender: TObject);
var
IdHTTP: TIdHTTP;
RequestData: TStringStream;
ResponseData: string;
JsonResponse: TJSONObject;
begin
IdHTTP := TIdHTTP.Create;
RequestData := TStringStream.Create;
try
IdHTTP.Request.ContentType := 'application/json';
RequestData.WriteString('{"messages":[{"content":"You are a helpful assistant.","role":"system"},{"content":"How are you?","role":"user"}],"model":'+
'"tinyllama-1.1b","stream":true,"max_tokens":2048,"stop":["hello","Hello","HELLO","hELLO"],"frequency_penalty":0,"presence_penalty":0,"temperature":0.7,"top_p":0.95}');
ResponseData := IdHTTP.Post('http://localhost:1337/v1/chat/completions', RequestData);
Memo2.Lines.Add(ResponseData);
JsonResponse := TJSONObject.ParseJSONValue(ResponseData) as TJSONObject;
finally
IdHTTP.Free;
RequestData.Free;
end;
end;
I sent an array, stream, HTTP 200 response OK, but this is the best I could get. Unfortunately, I can’t send screenshots, but the HTTP analyzer for Delphi and the code for Python work correctly, but the answer in Delphi is not JSON.
Here is a valid Python query that works in my case.
import requests
url = "http://localhost:1337/v1/chat/completions"
payload = {
"messages": [
{
"content": "You are a helpful assistant.",
"role": "system"
},
{
"content": "Hello!",
"role": "user"
}
],
"model": "tinyllama-1.1b",
"stream": True,
"max_tokens": 2048,
"stop": ["hello", "Hello", "HELLO", "hELLO"],
"frequency_penalty": 0,
"presence_penalty": 0,
"temperature": 0.7,
"top_p": 0.95
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
And I get this correct answer. But in Delphi the answer is as in the screenshot.
{
"choices": [
{
"finish_reason": null,
"index": 0,
"message": {
"content": "Hello user. What can I help you with?",
"role": "assistant"
}
}
],
"created": 1700193928,
"id": "ebwd2niJvJB1Q2Whyvkz",
"model": "_",
"object": "chat.completion",
"system_fingerprint": "_",
"usage": {
"completion_tokens": 500,
"prompt_tokens": 33,
"total_tokens": 533
}
}