From a0fd39e68d77159dde77c4b5d4a2f5ac17460f83 Mon Sep 17 00:00:00 2001 From: Dr Nic Williams Date: Thu, 25 Apr 2024 20:00:22 +1000 Subject: [PATCH] Help groq only return JSON --- examples/README.md | 13 +++++-------- examples/streaming-to-json-objects.rb | 4 +++- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/examples/README.md b/examples/README.md index 4c69cc6..a417890 100644 --- a/examples/README.md +++ b/examples/README.md @@ -50,20 +50,17 @@ Return JSON object for each one: { "name": "Mercury", "position": 1, "description": "Mercury is ..." } Between each response, say "NEXT" to clearly delineate each JSON response. + +Don't say anything else except the JSON objects above. ``` The code in the repo uses the `NEXT` token to know when to process the JSON object. -The output will look like, with each Ruby Hash object been pretty printed when it has been built from chunks. +The output will look like, with each JSON object printed (or saved to DB) obly when it has been completely built from chunks. ```json -{"name"=>"Mercury", - "position"=>1, - "description"=>"Mercury is the smallest planet in our solar system, with a highly elliptical orbit that takes it extremely close to the sun."} -{"name"=>"Venus", - "position"=>2, - "description"=> - "Venus is often called Earth's twin due to their similar size and mass, but it has a thick atmosphere that traps heat, making it the hottest planet."} +{"name":"Mercury","position":1,"description":"Mercury is the smallest planet in our solar system, with a highly elliptical orbit that takes it extremely close to the sun."} +{"name":"Venus","position":2,"description":"Venus is often called Earth's twin due to their similar size and mass, but it has a thick atmosphere that traps heat, making it the hottest planet."} ``` ### Pizzeria diff --git a/examples/streaming-to-json-objects.rb b/examples/streaming-to-json-objects.rb index 4acf89b..f54f1ca 100755 --- a/examples/streaming-to-json-objects.rb +++ b/examples/streaming-to-json-objects.rb @@ -49,6 +49,8 @@ { "name": "Mercury", "position": 1, "description": "Mercury is ..." } Between each response, say "NEXT" to clearly delineate each JSON response. + + Don't say anything else except the JSON objects above. TEXT # Handle each JSON object once it has been fully streamed @@ -63,7 +65,7 @@ def call(content) json = JSON.parse(@buffer) # do something with JSON, e.g. save to database - pp json + puts json.to_json # reset buffer @buffer = ""