Spaces:
Sleeping
Sleeping
Delete llm_utils.py
Browse files- llm_utils.py +0 -203
llm_utils.py
DELETED
@@ -1,203 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import google.generativeai as genai
|
3 |
-
from openai import OpenAI
|
4 |
-
from anthropic import Anthropic
|
5 |
-
import httpx
|
6 |
-
|
7 |
-
# Desteklenen LLM Sağlayıcıları ve Modelleri
|
8 |
-
LLM_PROVIDERS = {
|
9 |
-
"Gemini": {
|
10 |
-
"models": [
|
11 |
-
"gemini-pro",
|
12 |
-
"gemini-1.5-pro-latest",
|
13 |
-
"gemini-1.5-flash-latest"
|
14 |
-
],
|
15 |
-
"api_key_env": "GEMINI_API_KEY"
|
16 |
-
},
|
17 |
-
"OpenAI": {
|
18 |
-
"models": [], # Dinamik olarak çekilecek
|
19 |
-
"api_key_env": "OPENAI_API_KEY"
|
20 |
-
},
|
21 |
-
"Anthropic": {
|
22 |
-
"models": [], # Dinamik olarak çekilecek
|
23 |
-
"api_key_env": "ANTHROPIC_API_KEY"
|
24 |
-
},
|
25 |
-
"OpenRouter": {
|
26 |
-
"models": [], # Dinamik olarak çekilecek
|
27 |
-
"api_key_env": "OPENROUTER_API_KEY"
|
28 |
-
}
|
29 |
-
}
|
30 |
-
|
31 |
-
def get_llm_models(provider_name, api_key):
|
32 |
-
"""Seçilen sağlayıcının modellerini dinamik olarak çeker."""
|
33 |
-
models = []
|
34 |
-
if provider_name == "Gemini":
|
35 |
-
# Gemini modelleri statik olarak tanımlanmıştır, API'den çekmeye gerek yok
|
36 |
-
models = LLM_PROVIDERS["Gemini"]["models"]
|
37 |
-
elif provider_name == "OpenAI":
|
38 |
-
try:
|
39 |
-
client = OpenAI(api_key=api_key)
|
40 |
-
response = client.models.list()
|
41 |
-
models = [model.id for model in response.data if "gpt" in model.id and "vision" not in model.id and "instruct" not in model.id]
|
42 |
-
models.sort()
|
43 |
-
except Exception as e:
|
44 |
-
print(f"OpenAI modelleri çekilirken hata oluştu: {e}")
|
45 |
-
models = []
|
46 |
-
elif provider_name == "Anthropic":
|
47 |
-
try:
|
48 |
-
client = Anthropic(api_key=api_key)
|
49 |
-
# Anthropic API'sinde modelleri listeleme endpoint'i yok, bilinenleri manuel ekle
|
50 |
-
models = [
|
51 |
-
"claude-3-opus-20240229",
|
52 |
-
"claude-3-sonnet-20240229",
|
53 |
-
"claude-3-haiku-20240307"
|
54 |
-
]
|
55 |
-
except Exception as e:
|
56 |
-
print(f"Anthropic modelleri çekilirken hata oluştu: {e}")
|
57 |
-
models = []
|
58 |
-
elif provider_name == "OpenRouter":
|
59 |
-
try:
|
60 |
-
# OpenRouter API'sinden modelleri çek
|
61 |
-
headers = {"Authorization": f"Bearer {api_key}"}
|
62 |
-
response = httpx.get("https://openrouter.ai/api/v1/models", headers=headers)
|
63 |
-
response.raise_for_status()
|
64 |
-
data = response.json()
|
65 |
-
models = [model["id"] for model in data["data"]]
|
66 |
-
models.sort()
|
67 |
-
except Exception as e:
|
68 |
-
print(f"OpenRouter modelleri çekilirken hata oluştu: {e}")
|
69 |
-
models = []
|
70 |
-
return models
|
71 |
-
|
72 |
-
def validate_api_key(provider_name, api_key):
|
73 |
-
"""Seçilen sağlayıcının API anahtarını doğrular."""
|
74 |
-
if not api_key:
|
75 |
-
return False, "API Anahtarı boş olamaz."
|
76 |
-
|
77 |
-
try:
|
78 |
-
if provider_name == "Gemini":
|
79 |
-
genai.configure(api_key=api_key)
|
80 |
-
# Küçük bir model çağrısı ile anahtarı doğrula
|
81 |
-
model = genai.GenerativeModel('gemini-pro')
|
82 |
-
model.generate_content("test")
|
83 |
-
return True, "API Anahtarı Geçerli!"
|
84 |
-
elif provider_name == "OpenAI":
|
85 |
-
client = OpenAI(api_key=api_key)
|
86 |
-
client.models.list() # Modelleri listelemek anahtarı doğrular
|
87 |
-
return True, "API Anahtarı Geçerli!"
|
88 |
-
elif provider_name == "Anthropic":
|
89 |
-
client = Anthropic(api_key=api_key)
|
90 |
-
client.messages.create(
|
91 |
-
model="claude-3-haiku-20240307", # En küçük model
|
92 |
-
max_tokens=1,
|
93 |
-
messages=[{"role": "user", "content": "hi"}]
|
94 |
-
)
|
95 |
-
return True, "API Anahtarı Geçerli!"
|
96 |
-
elif provider_name == "OpenRouter":
|
97 |
-
headers = {"Authorization": f"Bearer {api_key}"}
|
98 |
-
response = httpx.get("https://openrouter.ai/api/v1/models", headers=headers)
|
99 |
-
response.raise_for_status() # HTTP 2xx dışında bir durum kodu hata fırlatır
|
100 |
-
return True, "API Anahtarı Geçerli!"
|
101 |
-
else:
|
102 |
-
return False, "Bilinmeyen sağlayıcı."
|
103 |
-
except Exception as e:
|
104 |
-
return False, f"API Anahtarı Geçersiz veya Bir Hata Oluştu: {e}"
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
def call_llm(provider_name, model_name, api_key, prompt):
|
111 |
-
"""Seçilen LLM sağlayıcısını kullanarak bir çağrı yapar."""
|
112 |
-
try:
|
113 |
-
if provider_name == "Gemini":
|
114 |
-
genai.configure(api_key=api_key)
|
115 |
-
model = genai.GenerativeModel(model_name)
|
116 |
-
response = model.generate_content(prompt)
|
117 |
-
return response.text
|
118 |
-
elif provider_name == "OpenAI":
|
119 |
-
client = OpenAI(api_key=api_key)
|
120 |
-
response = client.chat.completions.create(
|
121 |
-
model=model_name,
|
122 |
-
messages=[
|
123 |
-
{"role": "user", "content": prompt}
|
124 |
-
]
|
125 |
-
)
|
126 |
-
return response.choices[0].message.content
|
127 |
-
elif provider_name == "Anthropic":
|
128 |
-
client = Anthropic(api_key=api_key)
|
129 |
-
response = client.messages.create(
|
130 |
-
model=model_name,
|
131 |
-
max_tokens=4000, # Yeterli token sağlamak için
|
132 |
-
messages=[
|
133 |
-
{"role": "user", "content": prompt}
|
134 |
-
]
|
135 |
-
)
|
136 |
-
return response.content[0].text
|
137 |
-
elif provider_name == "OpenRouter":
|
138 |
-
headers = {
|
139 |
-
"Authorization": f"Bearer {api_key}",
|
140 |
-
"Content-Type": "application/json"
|
141 |
-
}
|
142 |
-
data = {
|
143 |
-
"model": model_name,
|
144 |
-
"messages": [
|
145 |
-
{"role": "user", "content": prompt}
|
146 |
-
]
|
147 |
-
}
|
148 |
-
response = httpx.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=data)
|
149 |
-
response.raise_for_status()
|
150 |
-
return response.json()["choices"][0]["message"]["content"]
|
151 |
-
else:
|
152 |
-
return "Bilinmeyen LLM sağlayıcısı."
|
153 |
-
except Exception as e:
|
154 |
-
return f"LLM çağrısı sırasında hata oluştu: {e}"
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
import re
|
160 |
-
import zipfile
|
161 |
-
import io
|
162 |
-
|
163 |
-
def parse_llm_output(llm_output):
|
164 |
-
"""LLM çıktısını dosya yolları ve içerikleri olarak ayrıştırır."""
|
165 |
-
files = {}
|
166 |
-
# Her bir dosya başlığını yakalamak için regex
|
167 |
-
# Başlıklar: # context/01_persona.md, # context/02_project_overview.md vb.
|
168 |
-
# İçerik, bir sonraki başlığa veya string'in sonuna kadar devam eder.
|
169 |
-
matches = re.finditer(r'^#\s*(context/[\w\d_\-]+\.md)\s*\n', llm_output, re.MULTILINE)
|
170 |
-
|
171 |
-
last_end = 0
|
172 |
-
last_file_path = None
|
173 |
-
|
174 |
-
for match in matches:
|
175 |
-
current_file_path = match.group(1).strip()
|
176 |
-
current_start = match.end()
|
177 |
-
|
178 |
-
if last_file_path:
|
179 |
-
# Önceki dosyanın içeriğini al
|
180 |
-
content = llm_output[last_end:match.start()].strip()
|
181 |
-
files[last_file_path] = content
|
182 |
-
|
183 |
-
last_file_path = current_file_path
|
184 |
-
last_end = current_start
|
185 |
-
|
186 |
-
# Son dosyanın içeriğini ekle
|
187 |
-
if last_file_path:
|
188 |
-
content = llm_output[last_end:].strip()
|
189 |
-
files[last_file_path] = content
|
190 |
-
|
191 |
-
return files
|
192 |
-
|
193 |
-
def create_zip_from_files(files_dict):
|
194 |
-
"""Bir dosya sözlüğünden (yol:içerik) bir ZIP dosyası oluşturur ve baytlarını döndürür."""
|
195 |
-
zip_buffer = io.BytesIO()
|
196 |
-
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED, False) as zip_file:
|
197 |
-
for file_path, content in files_dict.items():
|
198 |
-
# ZIP içinde klasör yapısını korumak için
|
199 |
-
zip_file.writestr(file_path, content.encode("utf-8"))
|
200 |
-
zip_buffer.seek(0)
|
201 |
-
return zip_buffer.getvalue()
|
202 |
-
|
203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|