What am I doing wrong with this json formatted API call?

I’ve built several api integrations with OpenAI already, and haven’t had any API response issues until attempting to do a chat format call.

Here’s the conversation I had in the playground that I’m using as a reference point for the API Call:

System:

you are an expert marketing professional

User:

write a witty social media post about a popular subcategory of online marketing

Assistant:

“Email marketing is like a box of chocolates, you never know which one will get clicked first. But with the right strategy, you can have your subscribers begging for more! :love_letter::chocolate_bar: #EmailMarketing #SweetSuccess

User:

change the subcategory to seo

Assistant:

“SEO is like a treasure hunt, but instead of gold, you’re searching for the top spot on Google. Let’s crack the code and unlock the ultimate prize! :mag::trophy: #SEOSuccess #SearchForTheWin

Here’s my json formatted API call trying to replicate this same conversation in order to get a new final response:

{
    "model": "gpt-3.5-turbo",
    "messages": [
        {
			"role": "system",
			"content": "you are an expert social media marketer.",
            "role": "user",
			"content": "write a witty social media post about a popular subcategory of online marketing",
			"role": "assistant",
			"content": "Email marketing is like a box of chocolates, you never know which one will get clicked first. But with the right strategy, you can have your subscribers begging for more!",
			"role": "user",
			"content": "change the subcategory to seo"
        }
    ]
}

But when I use the above API call, the response I get is:

{
    "id": "chatcmpl-6rGxqT9rrFlAjv6pSbHsXYqaH0wOM",
    "object": "chat.completion",
    "created": 1678154614,
    "model": "gpt-3.5-turbo-0301",
    "usage": {
        "prompt_tokens": 13,
        "completion_tokens": 55,
        "total_tokens": 68
    },
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "\n\nI'm sorry, as an AI language model, I do not have access to the previous conversation or the context of the subcategory you are referring to. Can you please provide more information about the category or topic you want me to change the subcategory to SEO?"
            },
            "finish_reason": "stop",
            "index": 0
        }
    ]
}

So basically, it’s not processing the previous parts of the conversation that I included in the API call.

What am I doing wrong here?

2 Likes

Instead of…

Maybe try

You want to be as specific as possible, even if there’s some context a human could pick up.

What are your settings (temp, frequency/presence penalty, etc…) Those could be affecting it too… Are the settings the same in Playground and the API calls?

Hope this helps…

But the whole idea of the ChatGPT format is that it understands the context from previous messages. This works fine in the playground, so it seems reasonable to have it work the same via API call.

As far as temp, frequency, etc., I’m not even including those parameters.

You are wrapping all replies into one message object. So your message array only has one length. Instead, the correct syntax should be like this:

{
      "model": "gpt-3.5-turbo",
      "messages": [
        {
          "role": "system",
          "content": "you are an expert social media marketer."
        },
        {
          "role": "user",
          "content": "write a witty social media post about a popular subcategory of online marketing"
        },
        {
          "role": "assistant",
          "content": "Email marketing is like a box of chocolates, you never know which one will get clicked first. But with the right strategy, you can have your subscribers begging for more!"
        },
        {
          "role": "user",
          "content": "change the subcategory to seo"
        }
      ]
    }

I tried to run it on my computer and got the result:

SEO is like a secret superpower that can make your website fly higher than Superman! Just make sure to watch out for those pesky Kryptonite backlinks. seo #MarketingHumor

8 Likes

Good eye. I missed that!

Trying to multi-task. :wink:

Thanks for the assist.

2 Likes

@waiyip hmm… that seemed like the right answer, but here’s what I got in the API response when I tried it:

Raw response for the API 
Status code 400
{
    "error": {
        "message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please send an email to support@openai.com and include any relevant code you'd like help with.)",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}

Fixed it with a linter! Had to (a) include the quotation marks for “model” and also remove the commas after each message.

Thanks @waiyip for pointing me in the right direction!

1 Like

Glad it finally works :beers:
Probably because what I provided is a javascript code so it automatically added ‘,’ after the last element when I tried to auto-format. I will change my original answer for the correct json syntax.

2 Likes

Correct.

Works fine for me, @waiyip

Hope this helps.

:slight_smile:

Appendix:

irb(main):001:0> ChatConversation.pluck(:messages).last
   (0.4ms)  SELECT sqlite_version(*)
   (0.1ms)  SELECT "chat_conversations"."messages" FROM "chat_conversations"
=> 
[{"role"=>"system", "content"=>"you are an expert social media marketer"},
 {"role"=>"user", "content"=>"write a witty social media post about a popular subcategory of online marketing"},
 {"role"=>"assistant",
  "content"=>
   "Email marketing is like a box of chocolates, you never know which one will get clicked first. But with the right strategy, you can have your subscribers begging for more!"},
 {"role"=>"user", "content"=>"change the subcategory to seo"},
 {"role"=>"assistant",
  "content"=>
   "SEO is like playing hide and seek with search engines, and the trick is to make sure they can always find you. Keep your website optimized and watch as traffic comes out of hiding and finds its way to you!"}]

Final Note: Sending content to the chat API twice and using historical message data from the DB:

Turns out I just needed proper json syntax. Not sure what language yours is written in—I’m definitely not a developer. Just trying to use a nocode tool to make json api calls.

I code in Ruby, just like my user name here and what my profile mentions.

Gotcha.

For me, it’s much faster to write the code because I am a software developer and engineer. I have actually never used a “no code” tool (and don’t trust them to write clean code)

But I do use generative AI to draft short software methods (which I verify) and for software dev completion suggestions in VSC (which is very useful, too much so, I just signed up for the GitHub copilot monthly $10 fee after the 30 day trial period expires).

Glad you got it working, @dustinwstout

:slight_smile:

1 Like