Create OpenAI class as last changes on this file had not been saved
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import inspect
|
|
@@ -9,11 +10,21 @@ from smolagents import CodeAgent, HfApiModel
|
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
-
|
| 13 |
-
model = HfApiModel(api_key=HF_TOKEN)
|
| 14 |
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# --- Basic Agent Definition ---
|
| 19 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
@@ -51,6 +62,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 51 |
|
| 52 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 53 |
try:
|
|
|
|
| 54 |
agent = BasicAgent(tools=tools, model=model, planning_interval=3)
|
| 55 |
except Exception as e:
|
| 56 |
print(f"Error instantiating agent: {e}")
|
|
|
|
| 1 |
import os
|
| 2 |
+
import openai
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import inspect
|
|
|
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
| 13 |
+
OPEN_AI_KEY = os.getenv("OPEN_AI_KEY")
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
+
class OpenAIModel:
|
| 17 |
+
def __init__(self):
|
| 18 |
+
openai.api_key = OPEN_AI_KEY
|
| 19 |
+
|
| 20 |
+
def run(self, prompt: str) -> str:
|
| 21 |
+
response = openai.ChatCompletion.create(
|
| 22 |
+
model="gpt-3.5-turbo", # or "gpt-4"
|
| 23 |
+
messages=[{"role": "user", "content": prompt}],
|
| 24 |
+
max_tokens=300
|
| 25 |
+
)
|
| 26 |
+
return response["choices"][0]["message"]["content"]
|
| 27 |
+
|
| 28 |
|
| 29 |
# --- Basic Agent Definition ---
|
| 30 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
| 62 |
|
| 63 |
# 1. Instantiate Agent ( modify this part to create your agent)
|
| 64 |
try:
|
| 65 |
+
model = OpenAIModel()
|
| 66 |
agent = BasicAgent(tools=tools, model=model, planning_interval=3)
|
| 67 |
except Exception as e:
|
| 68 |
print(f"Error instantiating agent: {e}")
|