Browse Source

feat(ads): add OpenRouter support and configurable model parameters in AI evaluations

master
PouyaKhajavi 1 day ago
parent
commit
cf4a67320f
  1. 13
      backend/ads/tasks.py

13
backend/ads/tasks.py

@ -37,7 +37,12 @@ def evaluate_ad_with_ai(self, evaluation_id):
return return
try: try:
client = OpenAI(api_key=api_key)
# Support OpenRouter keys seamlessly
client_kwargs = {"api_key": api_key}
if api_key.startswith("sk-or-"):
client_kwargs["base_url"] = "https://openrouter.ai/api/v1"
client = OpenAI(**client_kwargs)
system_instruction = ( system_instruction = (
"You are an expert Iranian market analyst. Your job is to read listing descriptions " "You are an expert Iranian market analyst. Your job is to read listing descriptions "
@ -53,13 +58,17 @@ def evaluate_ad_with_ai(self, evaluation_id):
f"Ad Description:\n{ad.description}" f"Ad Description:\n{ad.description}"
) )
default_model = "openrouter/free" if api_key.startswith("sk-or-") else "gpt-4o-mini"
model_name = os.getenv("OPENAI_MODEL", default_model)
completion = client.beta.chat.completions.parse( completion = client.beta.chat.completions.parse(
model="gpt-4o-mini",
model=model_name,
messages=[ messages=[
{"role": "system", "content": system_instruction}, {"role": "system", "content": system_instruction},
{"role": "user", "content": user_content} {"role": "user", "content": user_content}
], ],
response_format=AdEvaluationResult, response_format=AdEvaluationResult,
max_tokens=300,
timeout=25 timeout=25
) )

Loading…
Cancel
Save