Spaces:
Runtime error
Runtime error
Update g1.py
Browse files
g1.py
CHANGED
@@ -2,18 +2,9 @@ import groq
|
|
2 |
import time
|
3 |
import json
|
4 |
import logging
|
5 |
-
from typing import
|
6 |
-
from telegram.ext import (
|
7 |
-
Updater,
|
8 |
-
CommandHandler,
|
9 |
-
MessageHandler,
|
10 |
-
filters, # Исправленный импорт
|
11 |
-
CallbackContext
|
12 |
-
)
|
13 |
-
from telegram import Update, Bot
|
14 |
from tqdm import tqdm
|
15 |
|
16 |
-
# Конфигурация
|
17 |
logging.basicConfig(
|
18 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
19 |
level=logging.INFO
|
@@ -22,119 +13,44 @@ logger = logging.getLogger(__name__)
|
|
22 |
|
23 |
class ResponseGenerator:
|
24 |
def __init__(self):
|
25 |
-
self.
|
26 |
-
'iterative': "llama3-70b-8192",
|
27 |
-
'final': "llama3-70b-8192",
|
28 |
-
'code': "llama3-8b-8192"
|
29 |
-
}
|
30 |
self.max_tokens = {
|
31 |
'intermediate': 2000,
|
32 |
'final': 1000
|
33 |
}
|
34 |
|
35 |
-
def
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
if not is_final:
|
49 |
-
params["response_format"] = {"type": "json_object"}
|
50 |
-
|
51 |
-
response = groq.Groq().chat.completions.create(**params)
|
52 |
-
content = response.choices[0].message.content
|
53 |
-
|
54 |
-
if is_final:
|
55 |
-
return content
|
56 |
-
|
57 |
-
try:
|
58 |
-
parsed = json.loads(content)
|
59 |
-
if not all(k in parsed for k in ("title", "content", "next_action")):
|
60 |
-
raise ValueError("Missing required keys")
|
61 |
-
return parsed
|
62 |
-
except json.JSONDecodeError as e:
|
63 |
-
raise ValueError(f"Invalid JSON: {e}")
|
64 |
-
|
65 |
-
except Exception as e:
|
66 |
-
logger.error(f"Attempt {attempt + 1} failed: {e}")
|
67 |
-
if attempt == 2:
|
68 |
-
return {
|
69 |
-
"title": "Error",
|
70 |
-
"content": str(e),
|
71 |
-
"next_action": "final_answer"
|
72 |
-
}
|
73 |
-
time.sleep(2 ** attempt)
|
74 |
|
75 |
def generate_response(self, prompt: str) -> Generator:
|
76 |
-
|
77 |
-
|
78 |
-
"role": "
|
79 |
-
|
80 |
-
}
|
81 |
-
|
82 |
-
messages = [system_msg, {"role": "user", "content": prompt}]
|
83 |
-
steps = []
|
84 |
|
85 |
-
with tqdm(total=
|
86 |
-
for
|
87 |
-
|
88 |
-
|
89 |
-
elapsed = time.time() - start
|
90 |
-
|
91 |
-
step = {
|
92 |
-
"title": step_data.get("title", "Step"),
|
93 |
-
"content": step_data.get("content", ""),
|
94 |
-
"time": elapsed
|
95 |
-
}
|
96 |
-
steps.append(step)
|
97 |
-
|
98 |
pbar.update(1)
|
99 |
-
|
100 |
-
messages.append({"role": "assistant", "content": json.dumps(step_data)})
|
101 |
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
# Финальный ответ
|
106 |
-
messages.append({"role": "user", "content": "Дай окончательный ответ в чистом тексте."})
|
107 |
-
final_answer = self.make_api_call(messages, self.max_tokens['final'], is_final=True)
|
108 |
-
|
109 |
-
return {
|
110 |
-
"steps": steps,
|
111 |
-
"final_answer": final_answer,
|
112 |
-
"total_time": sum(s['time'] for s in steps)
|
113 |
-
}
|
114 |
-
|
115 |
-
# Gradio интерфейс
|
116 |
-
def format_response(response):
|
117 |
-
formatted = ""
|
118 |
-
for step in response['steps']:
|
119 |
-
formatted += f"### {step['title']}\n{step['content']}\n\n"
|
120 |
-
formatted += f"**Финальный ответ:**\n{response['final_answer']}\n\n"
|
121 |
-
formatted += f"**Общее время: {response['total_time']:.2f}с**"
|
122 |
-
return formatted
|
123 |
|
124 |
def gradio_interface(prompt: str) -> str:
|
125 |
-
|
126 |
-
response =
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
import gradio as gr
|
131 |
-
|
132 |
-
demo = gr.Interface(
|
133 |
-
fn=gradio_interface,
|
134 |
-
inputs=gr.Textbox(lines=3, label="Ваш запрос"),
|
135 |
-
outputs=gr.Markdown(label="Результат"),
|
136 |
-
title="🧠 G1: Цепочки рассуждений с Llama-3",
|
137 |
-
description="Генерация пошаговых рассуждений с использованием Groq API"
|
138 |
-
)
|
139 |
-
|
140 |
-
demo.launch()
|
|
|
2 |
import time
|
3 |
import json
|
4 |
import logging
|
5 |
+
from typing import Generator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from tqdm import tqdm
|
7 |
|
|
|
8 |
logging.basicConfig(
|
9 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
10 |
level=logging.INFO
|
|
|
13 |
|
14 |
class ResponseGenerator:
|
15 |
def __init__(self):
|
16 |
+
self.model = "llama3-70b-8192"
|
|
|
|
|
|
|
|
|
17 |
self.max_tokens = {
|
18 |
'intermediate': 2000,
|
19 |
'final': 1000
|
20 |
}
|
21 |
|
22 |
+
def _make_api_call(self, messages, max_tokens, is_final=False):
|
23 |
+
try:
|
24 |
+
response = groq.Groq().chat.completions.create(
|
25 |
+
model=self.model,
|
26 |
+
messages=messages,
|
27 |
+
max_tokens=max_tokens,
|
28 |
+
temperature=0.5,
|
29 |
+
**({"response_format": {"type": "json_object"}} if not is_final else {})
|
30 |
+
return response.choices[0].message.content
|
31 |
+
except Exception as e:
|
32 |
+
logger.error(f"API Error: {str(e)}")
|
33 |
+
return str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def generate_response(self, prompt: str) -> Generator:
|
36 |
+
messages = [
|
37 |
+
{"role": "system", "content": "You're an AI assistant. Explain your reasoning step-by-step in JSON format."},
|
38 |
+
{"role": "user", "content": prompt}
|
39 |
+
]
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
with tqdm(total=5, desc="Generating response") as pbar:
|
42 |
+
for _ in range(5):
|
43 |
+
result = self._make_api_call(messages, self.max_tokens['intermediate'])
|
44 |
+
messages.append({"role": "assistant", "content": result})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
pbar.update(1)
|
46 |
+
yield result
|
|
|
47 |
|
48 |
+
final_answer = self._make_api_call(messages, self.max_tokens['final'], is_final=True)
|
49 |
+
yield final_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
def gradio_interface(prompt: str) -> str:
|
52 |
+
gen = ResponseGenerator()
|
53 |
+
response = []
|
54 |
+
for chunk in gen.generate_response(prompt):
|
55 |
+
response.append(chunk)
|
56 |
+
return "\n\n".join(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|