File size: 6,451 Bytes
9a1fdaa 8333a7c 9a1fdaa 9701a91 9a1fdaa | 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | import os
import json
from pathlib import Path
from typing import Annotated
from autogen import AssistantAgent, UserProxyAgent
from autogen.coding import LocalCommandLineCodeExecutor
import gradio as gr
from autogen import ConversableAgent
from autogen import register_function
import mysql.connector
import random
import requests
from groq import Groq
from dotenv import load_dotenv
import urllib.parse
#tool_resp = ""
tool_topic = ""
js = """
function createGradioAnimation() {
var container = document.createElement('div');
container.id = 'gradio-animation';
container.style.fontSize = '2em';
container.style.fontWeight = 'bold';
container.style.textAlign = 'center';
container.style.marginBottom = '20px';
var text = 'wiki分析';
for (var i = 0; i < text.length; i++) {
(function(i){
setTimeout(function(){
var letter = document.createElement('span');
var randomColor = "#" + Math.floor(Math.random() * 16777215).toString(16);
letter.style.color = randomColor;
letter.style.opacity = '0';
letter.style.transition = 'opacity 0.5s';
letter.innerText = text[i];
container.appendChild(letter);
setTimeout(function() {
letter.style.opacity = '1';
}, 50);
// Blink the text 3 times
for (var j = 0; j < 3; j++) {
setTimeout(function() {
letter.style.opacity = '0';
}, 500 + j * 1000);
setTimeout(function() {
letter.style.opacity = '1';
}, 1000 + j * 1000);
}
}, i * 250);
})(i);
}
var gradioContainer = document.querySelector('.gradio-container');
gradioContainer.insertBefore(container, gradioContainer.firstChild);
return 'Animation created';
}
"""
load_dotenv(verbose=True)
conn = mysql.connector.connect(
host=os.environ.get("HOST"),
user=os.environ.get("USER_NAME"),
password=os.environ.get("PASSWORD"),
port=os.environ.get("PORT"),
database=os.environ.get("DB"),
ssl_disabled=True,
connection_timeout=60,
use_pure=True
)
cursor = conn.cursor(dictionary=True)
def get_rounrobin():
select_one_data_query = "select api from agentic_apis_count order by counts ASC"
cursor.execute(select_one_data_query)
result = cursor.fetchall()
first_api = result[0]['api']
return first_api
# MySQLに接続
def get_api_keys():
token = get_rounrobin()
os.environ["GROQ_API_KEY"] = token
return token
# Configure Groq
config_list = [{
"model": "llama-3.3-70b-versatile",
"api_key": os.environ["GROQ_API_KEY"],
"api_type": "groq"
}]
# Create a directory to store code files from code executor
work_dir = Path("coding")
work_dir.mkdir(exist_ok=True)
code_executor = LocalCommandLineCodeExecutor(work_dir=work_dir)
# Define wiki tool
def get_wiki_content(topic):
"""Get the info for some material"""
global tool_topic
print("parms:",tool_topic)
tmp_url = urllib.parse.unquote('https://ja.wikipedia.org/api/rest_v1/page/summary/'+tool_topic)
#print("tmp_url:",tmp_url)
data = requests.get(tmp_url)
#print('request:',data.content.decode("utf-8"))
# 元のデータ
datas = json.loads(data.content.decode("utf-8"))
#print('jload:',datas)
# 指定された形式に変換
#revenue_data = {item["title"]: {"revenue": item["extract"]} for item in datas}
#print("revenue data:",revenue_data)
jdump = json.dumps({
"title": datas["title"],
"content": datas["extract"],
})
#print("jdump:",jdump)
return jdump
# Create an AI assistant that uses the kpi tool
assistant = AssistantAgent(
#assistant = ConversableAgent(
name="groq_assistant",
system_message="""あなたは、次のことができる役に立つAIアシスタントです。
- 情報検索ツールを使用する
- 結果を分析して自然言語のみで説明する""",
llm_config={"config_list": config_list}
)
# Create a user proxy agent that only handles code execution
user_proxy = UserProxyAgent(
#user_proxy = ConversableAgent(
name="user_proxy",
human_input_mode="NEVER",
code_execution_config={"work_dir":"coding", "use_docker":False},
max_consecutive_auto_reply=2,
#llm_config={"config_list": config_list}
)
# Register weather tool with the assistant
@user_proxy.register_for_execution()
@assistant.register_for_llm(description="extractの内容")
def revenue_analysis(
title: Annotated[str, "title"],
topic: Annotated[str, "topic"]
) -> str:
#global tool_topic
print("parameters:",title,topic)
wiki_details = get_wiki_content(title)
wikis = json.loads(wiki_details)
print("myRevenue:",wikis)
return f"{wikis['title']}の内容は{wikis['content']}"
def get_wiki_and_respond(prompt,topic):
get_api_keys()
print("mytopic-is:",topic)
global tool_topic
tool_topic = topic
# Start the conversation
resp = user_proxy.initiate_chat(
assistant,
message=f"""3つのことをやってみましょう:
1. {prompt}の内容をtoolを利用して抽出します。
2. toolを利用して抽出された内容を詳しく分析します。
3. 日本語で説明してください。
"""
)
total_tokens = resp.cost['usage_including_cached_inference']['llama-3.3-70b-versatile']['total_tokens']
groq_assistant_contents = [entry['content'] for entry in resp.chat_history if entry['role'] == 'user' and entry['name'] == 'groq_assistant']
usages = "使用トークン数: "+str(total_tokens)
return groq_assistant_contents,usages
# Create Gradio interface
iface = gr.Interface(
js=js,
fn=get_wiki_and_respond,
inputs=[gr.Textbox(label="プロンプト",value="アユタヤ王朝とは何ですか?"),gr.Textbox(label="topic",value="アユタヤ王朝")],
outputs=[gr.Textbox(label="結果"),gr.Textbox(label="Usageデータ")],
title="資料の分析",
description="プロンプトを入力してデータを取得し、内容を分析します。",
submit_btn="実行",
clear_btn="クリア",
flagging_mode="never"
)
iface.launch()
|