4 ms·
The Python API example looks like it has been written by an LLM. You don't need to import json, you don't need to set the content type and it is good practice t
by threeducks 1y ago
The Python API example looks like it has been written by an LLM. You don't need to import json, you don't need to set the content type and it is good practice to use context managers ("with" statement) to release the connection in case of exceptions. Also, you don't gain anything by commenting variables with the name of the variable.
The following sample (probably) does the same thing and is almost half as short. I have not tested it because there is no signup (EDIT: I was mistaken, there actually is a "signup" behind the login link, which is Google or GitHub login, so the naming makes sense. I confused it with a previously more prominent waitlist link.)
import requests
# Your Hypermode Workspace API key
api_key = "<YOUR_HYP_WKS_KEY>"
# Use the Hypermode Model Router API endpoint
url = f"https://models.hypermode.host/v1/chat/completions"
headers = {"Authorization": f"Bearer {api_key}"}
payload = {
"model": "meta-llama/llama-4-scout-17b-16e-instruct",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is Dgraph?"},
],
"max_tokens": 150,
"temperature": 0.7,
}
# Make the API request
with requests.post(url, headers=headers, json=payload) as response:
response.raise_for_status()
print(response.json()["choices"][0]["message"]["content"])
- iamtherhino 1y agoSignups are open: hypermode.com/sign-up There's a waitlist for our prompt to agent product in the banner. That's a good call to update it to be more clear.
- threeducks 1y agoOh, I did not catch that. Sorry!
- iamtherhino 1y agoNot at all! I'm updating the banner now
- iamtherhino 1y agoupdated our python example too!
- KTibow 1y ago`post` automatically releases the connection. `with` only makes sense when you use a `requests.Session()`.
- threeducks 1y agoYou are right! https://github.com/psf/requests/blob/c65c780849563c891f35ffc98d3198b71011c012/src/requests/api.py#L58 https://github.com/psf/requests/blob/c65c780849563c891f35ffc... The post function calls request the request function which uses its own context manager that will call the close function of the connection object.