I’m trying to put together a mapping file for wire...
# help
e
I’m trying to put together a mapping file for wiremock to serve and I’m having trouble with jsonpath/regexes… I have json which looks like this:
Copy code
{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "system",
      "content": "You are a marvel comics writer, expert in all sorts of super heroes and super villains."
    },
    {
      "role": "user",
      "content": "Narrate the fight between a super hero and a super villain.\n\nDuring the narration, don't repeat \"super hero\" or \"super villain\".\n\nWrite 4 paragraphs maximum. Be creative.\n\nThe narration must be:\n- G rated\n- Workplace/family safe\n- No sexism, racism, or other bias/bigotry\n\nHere is the data you will use for the winner:\n\n+++++\nName: Chewbacca\nPowers: Big, hairy, strong\nLevel: 5\n+++++\n\nHere is the data you will use for the loser:\n\n+++++\nName: Wanderer\nPowers: Not strong\nLevel: 3\n+++++\n\nHere is the data you will use for the fight:\n\n+++++\nChewbacca who is a heroes has won the fight against Wanderer who is a .\n\nThe fight took place in Gotham City, which can be described as An American city rife with corruption and crime, the home of its iconic protector Batman..\n+++++\n"
    }
  ],
  "temperature": 0.9,
  "top_p": 0,
  "max_tokens": 1000,
  "presence_penalty": 0,
  "frequency_penalty": 0
}
And here is the mapping file:
Copy code
{
  "request": {
    "url": "/v1/chat/completions",
    "method": "POST",
    "headers": {
      "Accept": {
        "equalTo": "application/json",
        "caseInsensitive": true
      },
      "Content-Type": {
        "equalTo": "application/json",
        "caseInsensitive": true
      },
      "Authorization": {
        "matches": "Bearer .+"
      }
    },
    "bodyPatterns": [
      {
        "matchesJsonPath": "$.messages[?(@.role == 'user' && @.content.match(/^Narrate the fight between a super hero and a super villain/))]"
      }
    ]
  },
  "response": {
    "status": 200,
    "body": "{\n  \"id\": \"chatcmpl-8Uy1UC7ZFUXtjhYcY5VFj7eYGu6jX\",\n  \"object\": \"chat.completion\",\n  \"created\": 1702391260,\n  \"model\": \"gpt-3.5-turbo-0613\",\n  \"choices\": [\n    {\n      \"index\": 0,\n      \"message\": {\n        \"role\": \"assistant\",\n        \"content\": \"In the gritty streets of Gotham City, a clash of epic proportions unfolded. Han Solo, a hero known for his sharpshooting skills and unwavering skepticism towards the force, faced off against Storm Trooper, a villain armed with nothing more than a small gun. The odds seemed stacked against the Storm Trooper, but he was determined to prove his worth.\\n\\nAs the battle commenced, Han Solo swiftly dodged the Storm Trooper's feeble shots, his agility and experience shining through. With a smirk on his face, Han Solo aimed his big gun with precision, firing shots that echoed through the city. The Storm Trooper, though outmatched, refused to back down, his determination fueling his every move.\\n\\nWith each passing moment, Han Solo's level of expertise became more apparent. His shots were calculated and deadly, while the Storm Trooper struggled to keep up. The hero's confidence grew, his movements becoming more fluid and effortless. It was clear that the Storm Trooper's small gun was no match for Han Solo's superior firepower.\\n\\nIn a final, decisive moment, Han Solo's shot found its mark, incapacitating the Storm Trooper. The hero emerged victorious, his unwavering resolve prevailing over the villain's futile attempts. As the city rejoiced in the triumph of justice, Han Solo stood tall, a symbol of hope and resilience in the face of adversity.\"\n      },\n      \"finish_reason\": \"stop\"\n    }\n  ],\n  \"usage\": {\n    \"prompt_tokens\": 232,\n    \"completion_tokens\": 280,\n    \"total_tokens\": 512\n  },\n  \"system_fingerprint\": null\n}\n",
    "headers": {
      "Content-Type": "application/json",
      "openai-model": "gpt-3.5-turbo-0613",
      "openai-organization": "my-org-1234",
      "openai-version": "2020-10-01",
      "openai-processing-ms": "15000"
    }
  }
}
I’m having trouble on the
Copy code
{
  "matchesJsonPath": "$.messages[?(@.role == 'user' && @.content.match(/^Narrate the fight between a super hero and a super villain/))]"
}
Copy code
Request was not matched
                                               =======================

-----------------------------------------------------------------------------------------------------------------------
| Closest stub                                             | Request                                                  |
-----------------------------------------------------------------------------------------------------------------------
                                                           |
POST                                                       | POST
/v1/chat/completions                                       | /v1/chat/completions
                                                           |
Accept: application/json                                   | Accept: application/json
Content-Type: application/json                             | Content-Type: application/json
Authorization [matches] : Bearer .+                        | Authorization: Bearer my-key
                                                           |
$.messages[?(@.role == 'user' &&                           | {"model":"gpt-3.5-turbo","messages":[{"role":"system","co<<<<< Body does not match
@.content.match(/^Narrate the fight between a super hero   | ntent":"You are a marvel comics writer, expert in all
and a super villain/))]                                    | sorts of super heroes and super
                                                           | villains."},{"role":"user","content":"Narrate the fight
                                                           | between a super hero and a super villain.\n\nDuring the
                                                           | narration, don't repeat \"super hero\" or \"super
                                                           | villain\".\n\nWrite 4 paragraphs maximum. Be
                                                           | creative.\n\nThe narration must be:\n- G rated\n-
                                                           | Workplace/family safe\n- No sexism, racism, or other
                                                           | bias/bigotry\n\nHere is the data you will use for the
                                                           | winner:\n\n+++++\nName: Chewbacca\nPowers: Big, hairy,
                                                           | strong\nLevel: 5\n+++++\n\nHere is the data you will use
                                                           | for the loser:\n\n+++++\nName: Wanderer\nPowers: Not
                                                           | strong\nLevel: 3\n+++++\n\nHere is the data you will use
                                                           | for the fight:\n\n+++++\nChewbacca who is a heroes has
                                                           | won the fight against Wanderer who is a .\n\nThe fight
                                                           | took place in Gotham City, which can be described as An
                                                           | American city rife with corruption and crime, the home of
                                                           | its iconic protector
                                                           | Batman..\n+++++\n"}],"temperature":0.9,"top_p":0.0,"max_t
                                                           | okens":1000,"presence_penalty":0.0,"frequency_penalty":0.
                                                           | 0}
                                                           |
-----------------------------------------------------------------------------------------------------------------------
I’ve tried this in various jsonpath testers and it seems to match fine. I’ve even tried
$.messages[?((@.role == 'user') && (@.content =~ /^Narrate the fight between a super hero and a super villain/))]
and that doesn’t seem to work either
t
Might be easier to use a sub-matcher here e.g.
Copy code
{
  "matchesJsonPath": {
    "expression": "$.messages[?(@.role == 'user')].content",
    "contains": "Narrate the fight between a super hero and a super villain"
  }
}
e
That works!
thank you @Tom!
One follow-up @Tom If I change to use
matches
instead of
contains
, how do I escape a
.
character? For example, in something like this:
Copy code
{
  "matchesJsonPath": {
    "expression": "$.messages[?(@.role == 'user')].content",
    "matches": "Narrate the fight between a super hero and a super villain."
  }
}
How would I escape the period at the end? If I just use
\
I get errors on startup:
Copy code
"[truncated 3202 chars]; line: 34, column: 83]
        at com.github.tomakehurst.wiremock.standalone.JsonFileMappingsSource.loadMappingsInto(JsonFileMappingsSource.java:123)
        at com.github.tomakehurst.wiremock.core.WireMockApp.loadMappingsUsing(WireMockApp.java:265)
        at com.github.tomakehurst.wiremock.core.WireMockApp.loadDefaultMappings(WireMockApp.java:259)
        at com.github.tomakehurst.wiremock.core.WireMockApp.<init>(WireMockApp.java:124)
        at com.github.tomakehurst.wiremock.WireMockServer.<init>(WireMockServer.java:71)
Yet If I use
\\
to escape it, my matcher no longer works because one of the
\
characters is still there in the matcher:
Copy code
$.messages[?(@.role == 'user')].content [matches] Narrate  | Narrate the fight between a super hero and a super  <<<<< Body does not match
the fight between a super hero and a super                 | villain.
villain\.                                                  | During the narration, don't repeat "super hero"