Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from crewai import Agent, Task, Crew, LLM | |
| from crewai.tools import tool | |
| from groq import Groq | |
| import litellm.llms | |
| import requests | |
| import mysql.connector | |
| import os | |
| # データベース接続を設定 | |
| conn = mysql.connector.connect( | |
| host="www.ryhintl.com", | |
| user="smairuser", | |
| password="smairuser", | |
| port=36000, | |
| database="smair" | |
| ) | |
| # カーソルを取得 | |
| cursor = conn.cursor(dictionary=True) | |
| # APIキーを取得 | |
| select_one_data_query = "SELECT * FROM agentic_apis" | |
| cursor.execute(select_one_data_query) | |
| result = cursor.fetchall() | |
| keys = [item['key'] for item in result] | |
| # APIキーを設定 | |
| os.environ["GROQ_API_KEY"] = keys[2] | |
| llm = LLM(model="groq/llama-3.3-70b-versatile", api_key=os.environ["GROQ_API_KEY"]) | |
| tool_resp = "" | |
| def get_codoc_tool() -> str: | |
| """Get codoc tool""" | |
| response = requests.get('https://www.ryhintl.com/dbjson/getjson?sqlcmd=select `title` as caption,`snippet` as content from cohere_documents_auto') | |
| if response.status_code == 200: | |
| cohere_doc = response.content.decode('utf-8') | |
| global tool_resp | |
| tool_resp = cohere_doc | |
| return cohere_doc | |
| reporter = Agent( | |
| role='記事録分析家', | |
| goal='記事録の内容を詳しく分析する', | |
| backstory=( | |
| "記事録の内容をリアルタイムに分析することができない。" | |
| "よって、リアルタイムに記事録をタイムリーに分析してアドバイスしたい。" | |
| ), | |
| llm=llm, | |
| verbose=True | |
| ) | |
| summarizer = Agent( | |
| role='要約スペシアリスト', | |
| goal='toolで抽出された記事録データを的確に分析し、LLMを利用して答える。', | |
| backstory=( | |
| "記事録の内容を分析し、答えを得たい。" | |
| ), | |
| llm=llm, | |
| verbose=True | |
| ) | |
| report_task = Task( | |
| description=( | |
| "DB上の記事録データをリアルタイムに取得する。" | |
| ), | |
| expected_output='リアルタイムな記事録データを的確に返す。', | |
| agent=reporter, | |
| tools=[get_codoc_tool] | |
| ) | |
| summarizer_task = Task( | |
| description=( | |
| "記事録の内容を分析し、回答する。" | |
| ), | |
| expected_output='toolで抽出された記事録データを的確に分析し、LLMを利用して答える。', | |
| agent=summarizer, | |
| ) | |
| crew = Crew( | |
| agents=[reporter, summarizer], | |
| tasks=[report_task, summarizer_task], | |
| verbose=True | |
| ) | |
| def run_crew(ins): | |
| result = crew.kickoff() | |
| global tool_resp | |
| client = Groq(api_key=os.environ["GROQ_API_KEY"]) | |
| system_prompt = { | |
| "role": "system", | |
| "content": "You are a helpful assistant, answer questions concisely." | |
| } | |
| # Set the user prompt | |
| user_input = tool_resp+"を要約してください。" | |
| user_prompt = { | |
| "role": "user", "content": user_input | |
| } | |
| # Initialize the chat history | |
| chat_history = [system_prompt, user_prompt] | |
| response = client.chat.completions.create( | |
| model="llama-3.3-70b-versatile", | |
| messages=chat_history, | |
| max_tokens=1024, | |
| temperature=0) | |
| kekka = response.choices[0].message.content | |
| return "\n\n分析結果\n"+result.raw+"\n\nツールの分析:\n"+kekka | |
| iface = gr.Interface( | |
| fn=run_crew, | |
| inputs=[gr.Textbox(label="プロンプト",visible=False)], | |
| outputs=[gr.Textbox(label="結果")], | |
| title="資料の分析(CrewAI)", | |
| description="記事録の内容を分析します。", | |
| submit_btn="実行", | |
| clear_btn="クリア", | |
| flagging_mode="never" | |
| ) | |
| iface.launch() |