Skip to content

Commit

Permalink
Help groq only return JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
drnic committed Apr 25, 2024
1 parent 8d90413 commit a0fd39e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
13 changes: 5 additions & 8 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion examples/streaming-to-json-objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = ""
Expand Down

0 comments on commit a0fd39e

Please sign in to comment.