Retrieve Current Project ID and Trace Link in Python Script
import os
from typing import Optional
from httpx import Client
client = Client(base_url="http://localhost:6006")
query = """
query ($after: String = null) {
projects(after: $after) {
edges {
project: node {
id
name
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
"""
def get_id_for_project_name(name: str) -> Optional[str]:
has_next_page = True
after = None
while has_next_page:
response = client.post(
"/graphql", json={"query": query, "variables": {"after": after}}
)
if (status_code := response.status_code) != 200:
raise Exception(f"Failed with status code: {status_code}")
data = response.json()["data"]
edges = data["projects"]["edges"]
projects = map(lambda edge: edge["project"], edges)
for project in projects:
if project["name"] == name:
return project["id"]
page_info = data["projects"]["pageInfo"]
has_next_page = page_info["hasNextPage"]
after = page_info["endCursor"]
return None
def get_current_project_name() -> str:
project_name = os.getenv("CURRENT_PROJECT_NAME")
if not project_name:
raise Exception("Current project name is not set")
return project_name
# Set the current project name as an environment variable
os.environ["CURRENT_PROJECT_NAME"] = "your_current_project_name"
# Get the current project name
current_project_name = get_current_project_name()
# Get project ID dynamically for the current project
current_project_id = get_id_for_project_name(current_project_name)
if current_project_id is None:
raise Exception(f"Project ID for '{current_project_name}' not found")
print(f"Current Project ID: {current_project_id}")
This is the code provided by RUNLLM to get the current project I am working on. In the sense projects, it shows the different threads I created (shown in the attached image). So when I create a new thread(project) and I am working in that, how to get the thread(project) that I am currently working on to add the trace link ? The trace link looks like this ; http://localhost:{get_env_port()}/projects/{projectId}/traces/{traceId}
