skip to Main Content

I’ve created an OpenAI-powered platform for teachers in my district to create AI Bots that have thematic chats with students (i.e. why is socialism so polarizing in the US, etc). These chats are saved in a database, and a teacher can run queries against the dataset to gain macro-level insights into student understanding.

I’m using Guzzle to assemble the dataset, and then send it through the api to OpenAI. I’m successfully getting the responses I’m looking for, but the response contains no logical formatting. In other words, say I ask something like "List 3 students who gave creative answers and what each one said" – the response comes back in a single, solid paragraph, with no use of bulletpoints, dashes, etc.

I’m wondering where the fault lies – is this something I need to edit in Guzzle, or does my api request require tweaking? I can’t expect a teacher to add in "provide an HTML <br> tag following each student" in their request. It should be implied, much like the regular ChatGPT platform.

There’s no way for me to provide access to the whole platform, and copying it all here would be unwieldly. Let me know if you want any particular chunk of code provided and I will. I’m hoping someone who reads this happens to have had a similar issue.

Here’s the api request to get started. Many thanks for any suggestions!

    $response = $client->post('https://api.openai.com/v1/chat/completions', [
        'headers' => [
            'Authorization' => 'Bearer ' . $apiKey,
            'Content-Type' => 'application/json',
        ],
        'json' => [
            'model' => 'gpt-4o-mini', // Update model name here
            'messages' => [
                ['role' => 'system', 'content' => 'Start'],
                ['role' => 'user', 'content' => "Based on the following data:n$databaseResultsnnGenerate observations based on the query: $userQuery"]
            ],
            'max_tokens' => 1000,
        ],
    ]);

    $responseBody = json_decode($response->getBody(), true);
    $observations = $responseBody['choices'][0]['message']['content'];

    echo $observations;

2

Answers


  1. Regular ChatGPT and OpenAI API can differ very much in their responses to the same input. You will need to be very explicit in your query since OpenAI’s answers are very undeterministic.
    If e.g. you want a JSON, you can provide some minimal example of your valid JSON in your request.
    Maybe check this out and see if it helps anything:

    https://stackoverflow.com/a/78568572/5813473

    P.S. Beware – making a large or very complicated prompt not only costs more, but also can slow down OpenAI responses significantly.

    Login or Signup to reply.
  2. In your system prompt, add an instruction like "use markdown in your responses. Use bulleted lists whenever needed"

    Then, use a javascript library like https://github.com/markdown-it/markdown-it for parsing the markdown. Works brilliantly! I don’t know if you use a streaming response from OpenAi, but the markdown can work with that, too!

    Have fun.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search