-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote.py
More file actions
45 lines (40 loc) · 1.42 KB
/
Copy pathremote.py
File metadata and controls
45 lines (40 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
FIREWORKS_MODEL = os.getenv("FIREWORKS_MODEL", "accounts/fireworks/models/minimax-m3")
COST_PER_1K_TOKENS = 0.0009 # Fireworks AI pricing (USD)
def call_remote_model(query: str) -> dict:
"""
Sends query to Fireworks AI via OpenAI-compatible API.
Raises exception if remote is unavailable or times out.
"""
client = OpenAI(
api_key=os.getenv("FIREWORKS_API_KEY"),
base_url="https://api.fireworks.ai/inference/v1",
timeout=20.0
)
response = client.chat.completions.create(
model=FIREWORKS_MODEL,
messages=[
{
"role": "system",
"content": "You are a helpful AI assistant skilled in factual Q&A, mathematics, sentiment analysis, text summarization, named entity recognition, logic puzzles, and software engineering. Provide accurate and structured answers."
},
{
"role": "user",
"content": query
}
],
max_tokens=1024
)
answer = response.choices[0].message.content
tokens_used = response.usage.total_tokens
cost_incurred = (tokens_used / 1000) * COST_PER_1K_TOKENS
return {
"response": answer,
"model_used": FIREWORKS_MODEL,
"tokens": tokens_used,
"cost_saved": 0.0,
"cost_incurred": round(cost_incurred, 6)
}