File size: 3,562 Bytes
f51249a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6382930
f51249a
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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 = ""

@tool("get cohere docs")
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()