Spaces:
Running
Running
Update ai_providers.py
Browse files- ai_providers.py +10 -84
ai_providers.py
CHANGED
@@ -1,21 +1,10 @@
|
|
1 |
-
from abc import ABC, abstractmethod
|
2 |
from typing import Dict, Any, Optional
|
3 |
import os
|
4 |
from dotenv import load_dotenv
|
5 |
import time
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
@abstractmethod
|
10 |
-
def get_completion(self, prompt: str, **kwargs) -> str:
|
11 |
-
pass
|
12 |
-
|
13 |
-
@abstractmethod
|
14 |
-
def get_config(self) -> Dict[str, Any]:
|
15 |
-
pass
|
16 |
-
|
17 |
-
class GroqProvider(AIProvider):
|
18 |
-
def __init__(self, api_key: str, model: str = "deepseek-ai/deepseek-math-7b-instruct"):
|
19 |
self.api_key = api_key
|
20 |
self.model = model
|
21 |
|
@@ -60,76 +49,13 @@ class GroqProvider(AIProvider):
|
|
60 |
}]
|
61 |
}
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
def get_completion(self, prompt: str, **kwargs) -> str:
|
69 |
-
from openai import OpenAI # Updated import
|
70 |
-
|
71 |
-
client = OpenAI(api_key=self.api_key) # Updated client initialization
|
72 |
-
|
73 |
-
response = client.chat.completions.create(
|
74 |
-
model=self.model,
|
75 |
-
messages=[{"role": "user", "content": prompt}],
|
76 |
-
**kwargs
|
77 |
-
)
|
78 |
-
return response.choices[0].message.content
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
"config_list": [{
|
83 |
-
"model": self.model,
|
84 |
-
"api_key": self.api_key,
|
85 |
-
"temperature": 0.7,
|
86 |
-
"max_tokens": 4000
|
87 |
-
}]
|
88 |
-
}
|
89 |
-
|
90 |
-
class AnthropicProvider(AIProvider):
|
91 |
-
def __init__(self, api_key: str, model: str = "claude-3-opus-20240229"):
|
92 |
-
self.api_key = api_key
|
93 |
-
self.model = model
|
94 |
-
|
95 |
-
def get_completion(self, prompt: str, **kwargs) -> str:
|
96 |
-
import anthropic
|
97 |
-
|
98 |
-
client = anthropic.Client(api_key=self.api_key)
|
99 |
-
response = client.messages.create(
|
100 |
-
model=self.model,
|
101 |
-
messages=[{"role": "user", "content": prompt}],
|
102 |
-
**kwargs
|
103 |
-
)
|
104 |
-
return response.content[0].text
|
105 |
-
|
106 |
-
def get_config(self) -> Dict[str, Any]:
|
107 |
-
return {
|
108 |
-
"config_list": [{
|
109 |
-
"model": self.model,
|
110 |
-
"api_key": self.api_key,
|
111 |
-
"temperature": 0.7,
|
112 |
-
"max_tokens": 4000
|
113 |
-
}]
|
114 |
-
}
|
115 |
-
|
116 |
-
class AIProviderFactory:
|
117 |
-
@staticmethod
|
118 |
-
def create_provider(provider_type: str, api_key: Optional[str] = None, model: Optional[str] = None) -> AIProvider:
|
119 |
-
if not api_key:
|
120 |
-
load_dotenv()
|
121 |
-
|
122 |
-
providers = {
|
123 |
-
"openai": (OpenAIProvider, os.getenv("OPENAI_API_KEY"), "gpt-4"),
|
124 |
-
"groq": (GroqProvider, os.getenv("GROQ_API_KEY"), "deepseek-ai/deepseek-r1-distill-llama-70b"),
|
125 |
-
"anthropic": (AnthropicProvider, os.getenv("ANTHROPIC_API_KEY"), "claude-3-opus-20240229")
|
126 |
-
}
|
127 |
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
ProviderClass, default_api_key, default_model = providers[provider_type]
|
132 |
-
return ProviderClass(
|
133 |
-
api_key=api_key or default_api_key,
|
134 |
-
model=model or default_model
|
135 |
-
)
|
|
|
|
|
1 |
from typing import Dict, Any, Optional
|
2 |
import os
|
3 |
from dotenv import load_dotenv
|
4 |
import time
|
5 |
|
6 |
+
class GroqProvider:
|
7 |
+
def __init__(self, api_key: str, model: str = "deepseek-ai/deepseek-r1-distill-llama-70b"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
self.api_key = api_key
|
9 |
self.model = model
|
10 |
|
|
|
49 |
}]
|
50 |
}
|
51 |
|
52 |
+
def create_provider(api_key: Optional[str] = None, model: Optional[str] = None) -> GroqProvider:
|
53 |
+
if not api_key:
|
54 |
+
load_dotenv()
|
55 |
+
api_key = os.getenv("GROQ_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
if not api_key:
|
58 |
+
raise ValueError("GROQ_API_KEY must be set in environment variables")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
default_model = "deepseek-ai/deepseek-r1-distill-llama-70b"
|
61 |
+
return GroqProvider(api_key=api_key, model=model or default_model)
|
|
|
|
|
|
|
|
|
|
|
|