Skip to content

Instantly share code, notes, and snippets.

@UltiRequiem
Created December 9, 2025 18:57
Show Gist options
  • Select an option

  • Save UltiRequiem/453e7142f6d59eaa42e912ebecc0b394 to your computer and use it in GitHub Desktop.

Select an option

Save UltiRequiem/453e7142f6d59eaa42e912ebecc0b394 to your computer and use it in GitHub Desktop.
random hackathon i went
import nearai
import json
import requests
hub_url = "https://api.near.ai/v1"
assistant_id = "mrh2003.near/prueba/0.0.2"
# Login to NEAR AI Hub using nearai CLI.
# Read the auth object from ~/.nearai/config.json
auth = nearai.config.load_config_file()["auth"]
signature = json.dumps(auth)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {signature}"
}
# -----------------------------------------------------------
# 0) Crear el hilo (solo la primera vez)
# -----------------------------------------------------------
thread_resp = requests.post(
f"{hub_url}/threads",
headers=headers,
json={}
)
thread_id = thread_resp.json()["id"]
# -----------------------------------------------------------
# 1) Subir el archivo → file_id
# -----------------------------------------------------------
file_path = "dni.jpg"
with open(file_path, "rb") as f:
file_resp = requests.post(
f"{hub_url}/files",
headers={"Authorization": f"Bearer {signature}"},
files={"file": (file_path, f)},
data={"purpose": "assistants"}
)
file_id = file_resp.json()["id"]
# -----------------------------------------------------------
# 2) Añadir mensaje al hilo con el adjunto
# -----------------------------------------------------------
msg_url = f"{hub_url}/threads/{thread_id}/messages"
msg_body = {
"role": "user",
"content": "Extrae el texto",
"attachments": [{"file_id": file_id}]
}
answer = requests.post(
msg_url,
headers=headers,
json=msg_body
)
# -----------------------------------------------------------
# 3) Lanzar el run del asistente sobre ese hilo
# -----------------------------------------------------------
run_url = f"{hub_url}/threads/{thread_id}/runs"
run_body = {"assistant_id": assistant_id}
run_resp = requests.post(
run_url,
headers=headers,
json=run_body
)
print("run →", run_resp.json()["id"])
messages_url = f"{hub_url}/threads/{thread_id}/messages"
messages = requests.get(
messages_url,
headers=headers,
).json()
print(thread_id)
print(messages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment