fudii0921 commited on
Commit
f51249a
·
verified ·
1 Parent(s): 23d4a60

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +134 -0
app.py ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from crewai import Agent, Task, Crew, LLM
3
+ from crewai.tools import tool
4
+ from groq import Groq
5
+ import litellm.llms
6
+ import requests
7
+ import mysql.connector
8
+ import os
9
+
10
+ # データベース接続を設定
11
+ conn = mysql.connector.connect(
12
+ host="www.ryhintl.com",
13
+ user="smairuser",
14
+ password="smairuser",
15
+ port=36000,
16
+ database="smair"
17
+ )
18
+
19
+ # カーソルを取得
20
+ cursor = conn.cursor(dictionary=True)
21
+ # APIキーを取得
22
+ select_one_data_query = "SELECT * FROM agentic_apis"
23
+ cursor.execute(select_one_data_query)
24
+ result = cursor.fetchall()
25
+ keys = [item['key'] for item in result]
26
+
27
+ #litellm._turn_on_debug()
28
+
29
+ # APIキーを設定
30
+ #os.environ["OPENAI_API_KEY"] = keys[1]
31
+ os.environ["GROQ_API_KEY"] = keys[2]
32
+ #os.environ["COHERE_API_KEY"] = keys[3]
33
+
34
+ llm = LLM(model="groq/llama-3.3-70b-versatile", api_key=os.environ["GROQ_API_KEY"])
35
+ #llm = LLM(model="cohere/command-r-plus-04-2024", api_key=os.environ["COHERE_API_KEY"])
36
+
37
+ tool_resp = ""
38
+
39
+ @tool("get cohere docs")
40
+ def get_codoc_tool() -> str:
41
+ """Get codoc tool"""
42
+ response = requests.get('https://www.ryhintl.com/dbjson/getjson?sqlcmd=select `title` as caption,`snippet` as content from cohere_documents_auto')
43
+
44
+ if response.status_code == 200:
45
+ cohere_doc = response.content.decode('utf-8')
46
+
47
+ global tool_resp
48
+ tool_resp = cohere_doc
49
+
50
+ return cohere_doc
51
+
52
+ reporter = Agent(
53
+ role='記事録分析家',
54
+ goal='記事録の内容を詳しく分析する',
55
+ backstory=(
56
+ "記事録の内容をリアルタイムに分析することができない。"
57
+ "よって、リアルタイムに記事録をタイムリーに分析してアドバイスしたい。"
58
+ ),
59
+ llm=llm,
60
+ verbose=True
61
+ )
62
+
63
+ summarizer = Agent(
64
+ role='要約スペシアリスト',
65
+ goal='toolで抽出された記事録データを的確に分析し、LLMを利用して答える。',
66
+ backstory=(
67
+ "記事録の内容を分析し、答えを得たい。"
68
+ ),
69
+ llm=llm,
70
+ verbose=True
71
+ )
72
+
73
+ report_task = Task(
74
+ description=(
75
+ "DB上の記事録データをリアルタイムに取得する。"
76
+ ),
77
+ expected_output='リアルタイムな記事録データを的確に返す。',
78
+ agent=reporter,
79
+ tools=[get_codoc_tool]
80
+ )
81
+
82
+ summarizer_task = Task(
83
+ description=(
84
+ "記事録の内容を分析し、回答する。"
85
+ ),
86
+ expected_output='toolで抽出された記事録データを的確に分析し、LLMを利用して答える。',
87
+ agent=summarizer,
88
+ )
89
+
90
+ crew = Crew(
91
+ agents=[reporter, summarizer],
92
+ tasks=[report_task, summarizer_task],
93
+ verbose=True
94
+ )
95
+
96
+ def run_crew(ins):
97
+ result = crew.kickoff()
98
+ global tool_resp
99
+ client = Groq(api_key=os.environ["GROQ_API_KEY"])
100
+ system_prompt = {
101
+ "role": "system",
102
+ "content": "You are a helpful assistant, answer questions concisely."
103
+ }
104
+
105
+ # Set the user prompt
106
+ user_input = tool_resp+"を要約してください。"
107
+ user_prompt = {
108
+ "role": "user", "content": user_input
109
+ }
110
+
111
+ # Initialize the chat history
112
+ chat_history = [system_prompt, user_prompt]
113
+
114
+ response = client.chat.completions.create(
115
+ model="llama-3.3-70b-versatile",
116
+ messages=chat_history,
117
+ max_tokens=1024,
118
+ temperature=0)
119
+
120
+ kekka = response.choices[0].message.content
121
+ return "\n\n分析結果\n"+result.raw+"\n\nツールの分析:\n"+kekka
122
+
123
+ iface = gr.Interface(
124
+ fn=run_crew,
125
+ inputs=[gr.Textbox(label="プロンプト",visible=False)],
126
+ outputs=[gr.Textbox(label="結果")],
127
+ title="資料の分析",
128
+ description="記事録の内容を分析します。",
129
+ submit_btn="実行",
130
+ clear_btn="クリア",
131
+ flagging_mode="never"
132
+ )
133
+
134
+ iface.launch()