Create agent.py
#430
by BiGuan - opened
agent.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import requests
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from langchain_openai import ChatOpenAI
|
| 6 |
+
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
llm = ChatOpenAI(
|
| 10 |
+
model="qwen3.5-35b-a3b",
|
| 11 |
+
openai_api_key=os.getenv("AGICTO_API_KEY"),
|
| 12 |
+
openai_api_base=os.getenv("AGICTO_BASE_URL", "https://api.agicto.cn/v1"),
|
| 13 |
+
temperature=0.0,
|
| 14 |
+
max_tokens=256,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
SYSTEM_PROMPT = (
|
| 18 |
+
"Answer with one word or number.\n"
|
| 19 |
+
"No explanation.\n"
|
| 20 |
+
"No punctuation.\n"
|
| 21 |
+
"No units.\n"
|
| 22 |
+
"No tags."
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
def clean(text: str) -> str:
|
| 26 |
+
text = re.sub(r".*?", "", text, flags=re.S)
|
| 27 |
+
return text.strip().splitlines()[0]
|
| 28 |
+
|
| 29 |
+
def agent(question: str, files=None) -> str:
|
| 30 |
+
try:
|
| 31 |
+
context = ""
|
| 32 |
+
if files:
|
| 33 |
+
for f in files:
|
| 34 |
+
r = requests.get(f, timeout=10)
|
| 35 |
+
if r.ok:
|
| 36 |
+
context += r.text[:6000]
|
| 37 |
+
|
| 38 |
+
res = llm.invoke([
|
| 39 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 40 |
+
{"role": "user", "content": f"{context}{question}"}
|
| 41 |
+
])
|
| 42 |
+
|
| 43 |
+
return clean(res.content)
|
| 44 |
+
|
| 45 |
+
except Exception:
|
| 46 |
+
return "0"
|