Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create tokencount #376

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions tokencount
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import httpx

# Define the API URL and the dummy API key
api_url = "https://api.openai.com/v1/chat/completions"
api_key = "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" # Replace with your actual API key

# Define the headers including the Authorization header
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}

# Define the user message
#user_message = "List only the valid English words from these: ehTbs"

# Define the request payload with system and user messages
payload = {
"model": "gpt-4o-mini",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "List only the valid English words from these: ehTbs"
}
]
}

# Send the POST request to OpenAI's API
try:
response = httpx.post(api_url, json=payload, headers=headers)
response.raise_for_status() # Ensure the request was successful

# Parse the response from the API to get the number of tokens used
result = response.json()
tokens_used = result['usage']['total_tokens']
print(f"Number of tokens used: {tokens_used}")

except httpx.HTTPStatusError as e:
print(f"Request failed with status code {e.response.status_code}: {e.response.text}")