Helicone Community Page

Updated last year

hey Matt This is an example code snippet

hey Matt! This is an example code snippet to get the data for the last x hours. Feel free to change up the query slightly to fit your limit and filter

Plain Text
import requests
from datetime import datetime, timedelta

# Calculate the ISO string for one hour ago
one_hour_ago_iso = (datetime.utcnow() -
                    timedelta(hours=1)).isoformat() + "Z"

# Define the GraphQL endpoint (replace with your actual endpoint)
url = "https://www.helicone.ai/api/graphql"

# Define the GraphQL query and variables
query = """
query ExampleQuery($limit: Int, $offset: Int, $timeStampISO: String){
  heliconeRequest(
      limit: $limit
      offset: $offset
      filters: [
        {
          createdAt: {
            gte: $timeStampISO
          }
        }
      ]
  ) {
      prompt
      properties{
        name
      }
      responseBody
      response
  }
}
"""


MAX_LOOPS = 1000
PAGE_SIZE = 100
all_data = []
for i in range(MAX_LOOPS):
    print("Loop", i)
    variables = {
        "limit": PAGE_SIZE,
        "offset": PAGE_SIZE * i,
        "timeStampISO": one_hour_ago_iso
    }

    # Define headers with Authorization
    headers = {
        "Authorization": "Bearer <KEY>",
        "Content-Type": "application/json"
    }

    # Send the request with headers
    response = requests.post(
        url, json={'query': query, 'variables': variables}, headers=headers)

    # Check if the request was successful
    if response.status_code == 200:
        data = response.json()['data']['heliconeRequest']
        all_data.extend(data)
        print("Success!")
        if (len(data) < PAGE_SIZE):
            break
    else:
        print("Error:", response.status_code)
        print(response.text)


print(len(all_data))
m
a
10 comments
Thanks Scott! This is super helpful, however one bit I am unsure about is filtering by property? We use a property to differentiate between different purposes called "siteid" is there a way to pass up a property name/value pair to filter only those requests?

So in SQL terms something like:

WHERE siteid = 5

πŸ˜„
ah I see. the following code snippet should work:

Plain Text
query ExampleQuery($limit: Int, $offset: Int, ) {
  heliconeRequest(
    limit: $limit
    offset: $offset
    filters: [
      {
        property: {
          name: "test-flight",
          value: {
            equals: "Habibi"
          }
        }
      }
    ]
  ) {
    prompt
    properties {
      name
    }
    responseBody
    response
  }
}
replace "test-flight" and "Habibi" haha
Thanks Scott!!
for sure! If you got anything else for me, I'm more than happy to help!
Will do, thanks again!
@ayoKho The query is working well, although I have noticed its very slow... for example, one I just tried took 22 seconds to respond - I'm not using it for anything too intensive, but just thought I'd highlight it in case there's anything that can be optimised
thanks for letting us know! how many rows did the 22 second query take?
Only 10 - hence why it seemd particularly slow
oh interesting - yeah it shouldnt take that long for only 10 requests. thanks for letting us know!
Add a reply
Sign up and join the conversation on Discord