-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
72 lines (60 loc) · 2.04 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from flask import Flask, request, jsonify
from flask_cors import CORS
import openai
import os
from langchain.chat_models import ChatOpenAI
from dotenv import load_dotenv
from src.main import make_agent, evaluate_by_llm_with_criteria
# 環境変数のロード
load_dotenv()
# OpenAI APIキーの設定
openai.api_key = os.getenv("OPENAI_API_KEY")
app = Flask(__name__)
CORS(app)
# アプリ起動時に一度だけ初期化
p_tool_configs = [
"./docs/tools/code.yaml",
"./docs/tools/incident_rag.yaml",
"./docs/tools/knowledge_rag.yaml",
"./docs/tools/raw_text.yaml",
"./docs/tools/search.yaml",
]
p_react_prompt = "./docs/react_base_prompt.md"
k = 3
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
agent = make_agent(p_tool_configs, p_react_prompt, k, llm)
@app.route('/chat', methods=['POST'])
def chat():
user_input = request.json.get('message', '')
print(user_input)
if not user_input:
return jsonify({"error": "Message is required"}), 400
try:
# 毎回新規に初期化せず、既存のagentとllmを使いまわす
ai_response = agent.run(user_input)
return jsonify({"response": ai_response})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/evaluate', methods=['POST'])
def evaluate():
data = request.json
pred = data.get('pred', '')
answer = data.get('answer', '')
question = data.get('question', None)
criteria_with_weights = data.get('criteria_with_weights', [])
if not (pred and answer and criteria_with_weights):
return jsonify({"error": "Required fields: pred, answer, criteria_with_weights"}), 400
try:
# 同じllmを利用する
result = evaluate_by_llm_with_criteria(
pred=pred,
answer=answer,
llm=llm,
question=question,
criteria_with_weights=criteria_with_weights
)
return jsonify(result)
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=False)