alarv commited on
Commit
830adc3
·
1 Parent(s): 43b71ab

feat: use openai api

Browse files
Files changed (2) hide show
  1. README.md +16 -1
  2. app.py +25 -5
README.md CHANGED
@@ -16,4 +16,19 @@ tags:
16
  - agent-course
17
  ---
18
 
19
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  - agent-course
17
  ---
18
 
19
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
20
+
21
+ ## Environment Setup
22
+
23
+ Set the following environment variables before running/deploying:
24
+
25
+ - `OPENAI_API_KEY` (required): your OpenAI API key
26
+ - `LLM_MODEL_ID` (optional): defaults to `gpt-4o-mini`
27
+ - `LLM_API_BASE` (optional): defaults to `https://api.openai.com/v1`
28
+ - `LLM_MAX_TOKENS` (optional): max assistant output tokens, defaults to `4096`
29
+ - `LLM_TEMPERATURE` (optional): sampling temperature, defaults to `0.3`
30
+
31
+ Tracing (optional, if you use Langfuse as currently configured in `app.py`):
32
+
33
+ - `LANGFUSE_PUBLIC_KEY`
34
+ - `LANGFUSE_SECRET_KEY`
app.py CHANGED
@@ -451,12 +451,32 @@ final_answer = FinalAnswerTool()
451
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
452
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
453
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454
  model = OpenAIServerModel(
455
- model_id=os.environ["LLM_MODEL_ID"],
456
- api_base=os.environ["LLM_API_BASE"],
457
- api_key="none",
458
- max_tokens=2096,
459
- temperature=0.5,
460
  )
461
 
462
 
 
451
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
452
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
453
 
454
+ # Configure OpenAI model (defaults to gpt-4o-mini)
455
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
456
+ if not OPENAI_API_KEY:
457
+ raise RuntimeError(
458
+ "OPENAI_API_KEY environment variable is required for OpenAI API access."
459
+ )
460
+
461
+ # Read optional generation settings with safe defaults
462
+ _max_tokens_env = os.getenv("LLM_MAX_TOKENS")
463
+ try:
464
+ MAX_TOKENS = int(_max_tokens_env) if _max_tokens_env else 4096
465
+ except ValueError:
466
+ MAX_TOKENS = 4096
467
+
468
+ _temperature_env = os.getenv("LLM_TEMPERATURE")
469
+ try:
470
+ TEMPERATURE = float(_temperature_env) if _temperature_env else 0.3
471
+ except ValueError:
472
+ TEMPERATURE = 0.3
473
+
474
  model = OpenAIServerModel(
475
+ model_id=os.getenv("LLM_MODEL_ID", "gpt-4o-mini"),
476
+ api_base=os.getenv("LLM_API_BASE", "https://api.openai.com/v1"),
477
+ api_key=OPENAI_API_KEY,
478
+ max_tokens=MAX_TOKENS,
479
+ temperature=TEMPERATURE,
480
  )
481
 
482