Spaces:
Sleeping
Sleeping
Update src/models.py
Browse files- src/models.py +32 -7
src/models.py
CHANGED
@@ -12,22 +12,47 @@ load_dotenv(override=True)
|
|
12 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
13 |
anthropic_api_key = os.getenv("ANTHROPIC_API_KEY")
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Initialize disguised API clients
|
16 |
-
openai = InferenceClient(
|
17 |
-
claude = InferenceClient(
|
18 |
|
|
|
|
|
|
|
|
|
|
|
19 |
def get_gpt_completion(prompt, system_message):
|
20 |
try:
|
21 |
-
response = openai.
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
except Exception as e:
|
24 |
print(f"GPT error: {e}")
|
25 |
raise
|
26 |
|
|
|
27 |
def get_claude_completion(prompt, system_message):
|
28 |
try:
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
31 |
except Exception as e:
|
32 |
print(f"Claude error: {e}")
|
33 |
-
raise
|
|
|
12 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
13 |
anthropic_api_key = os.getenv("ANTHROPIC_API_KEY")
|
14 |
|
15 |
+
# Warn if any API key is missing
|
16 |
+
if not openai_api_key:
|
17 |
+
print("❌ OpenAI API Key is missing!")
|
18 |
+
|
19 |
+
if not anthropic_api_key:
|
20 |
+
print("❌ Anthropic API Key is missing!")
|
21 |
+
|
22 |
# Initialize disguised API clients
|
23 |
+
openai = InferenceClient(token=openai_api_key)
|
24 |
+
claude = InferenceClient(token=anthropic_api_key)
|
25 |
|
26 |
+
# Model names
|
27 |
+
OPENAI_MODEL = "mistralai/Mistral-7B-Instruct-v0.3"
|
28 |
+
CLAUDE_MODEL = "mistralai/Mistral-7B-Instruct-v0.3"
|
29 |
+
|
30 |
+
# Call OpenAI's GPT model with prompt and system message
|
31 |
def get_gpt_completion(prompt, system_message):
|
32 |
try:
|
33 |
+
response = openai.chat.completions.create(
|
34 |
+
model=OPENAI_MODEL,
|
35 |
+
messages=[
|
36 |
+
{"role": "system", "content": system_message},
|
37 |
+
{"role": "user", "content": prompt}
|
38 |
+
],
|
39 |
+
stream=False,
|
40 |
+
)
|
41 |
+
return response.choices[0].message.content
|
42 |
except Exception as e:
|
43 |
print(f"GPT error: {e}")
|
44 |
raise
|
45 |
|
46 |
+
# Call Anthropic's Claude model with prompt and system message
|
47 |
def get_claude_completion(prompt, system_message):
|
48 |
try:
|
49 |
+
result = claude.messages.create(
|
50 |
+
model=CLAUDE_MODEL,
|
51 |
+
max_tokens=2000,
|
52 |
+
system=system_message,
|
53 |
+
messages=[{"role": "user", "content": prompt}]
|
54 |
+
)
|
55 |
+
return result.content[0].text
|
56 |
except Exception as e:
|
57 |
print(f"Claude error: {e}")
|
58 |
+
raise
|