import cohere import numpy as np from pgvector.psycopg import register_vector, Bit import psycopg2 import asyncio import gradio as gr from pathlib import Path from typing import Annotated from autogen import AssistantAgent, UserProxyAgent from autogen.coding import LocalCommandLineCodeExecutor 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 os import json tool_resp = "" 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 ) cursor = conn.cursor(dictionary=True) def get_api_keys(): #token = get_rounrobin() token = os.environ["GROQ_API_KEY"] return token # Configure Groq config_list = [{ "model": "llama-3.3-70b-versatile", "api_key": os.environ["GROQ_API_KEY"], "api_type": "groq" }] co = cohere.ClientV2(api_key=os.environ["COHERE_API_KEY"]) # Database setup conn = psycopg2.connect( dbname=os.environ.get("DB"), user=os.environ.get("USER_NAME"), password=os.environ.get("PASSWORD"), host=os.environ.get("HOST"), port=os.environ.get("PGPORT") ) cur = conn.cursor() # 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 revenue tool def get_current_invitee(location): """Get the revenue for some material""" data = requests.get('https://www.ryhintl.com/dbjson/getjson?sqlcmd=select `id`,`username`,`email`,`division`,`position` from spfx_pinfo') # 元のデータ data = json.loads(data.content) # 指定された形式に変換 invitee_data = {item["username"]+"様": {"役職": item["position"], "部門": item["division"], "body": "詳細は以下の通りです。・日時:2025年6月1日(土)10時・場所:銀座プレアンタン・内容:新製品の紹介、実演、質疑応答などお忙しい中大変お手数ですが、ぜひ足を運んで頂ければと思っております。承知くださいます様、よろしくお願い申し上げます。"} for item in data} script_data = "" for item in data: script_data += f''' {item["username"]}様、 益々ご清祥のこととお喜び申し上げます。 平素は格別のご厚情を賜り、誠にありがとうございます。 先日はお忙しい中大変お世話になり、誠にありがとうございます。 私どもクリーンウォータ株式会社は、浄水器を製造販売する会社であります。 今回、弊社が新製品の『パーソナル浄水器』を御用達いただき、新商品の発表会を開催することになりました。 発表会の詳細は以下の通りです。 ・日時:2025年6月1日(土)10時 ・場所:銀座プレアンタン ・内容:新製品の紹介、実演、質疑応答など お忙しい中大変お手数ですが、ぜひ足を運んで頂ければと思っております。 承知くださいます様、よろしくお願い申し上げます。 以上、よろしくお願いいたします。 ''' print("print:",script_data) return script_data # 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="snippetの内容") def invitee_analysis( location: Annotated[str, "title"] ) -> str: invitee_details = get_current_invitee(location=location) print("after:",invitee_details) global tool_resp tool_resp = invitee_details return invitee_details def get_revenue_and_plot(event1): get_api_keys() # Start the conversation resp = user_proxy.initiate_chat( assistant, message=f"""3つのことをやってみましょう: 1. {event1}の内容を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'] global tool_resp print("final:",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_input = tool_resp+"を要約してください。招待状を作るpythonコードを生成してください。生成されるコードに、必ず、顧客リストをcsv形式のテキストで含めてください。" 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=4096, temperature=0) kekka = response.choices[0].message.content usages = "使用トークン数: "+str(total_tokens)+ " \n"+kekka tmp_resp = groq_assistant_contents[0] return tmp_resp,usages # Embedding function def embed(input, input_type): co = cohere.ClientV2(api_key="GqsxZlKmcBzSultkVOfKPf7kVhYkporXvivq9KHg") #response = co.embed(texts=input, model='embed-v4.0', input_type=input_type, embedding_types=['ubinary']) response = co.embed(texts=input, model='embed-multilingual-v3.0', input_type=input_type, embedding_types=['ubinary']) return [np.unpackbits(np.array(embedding, dtype=np.uint8)) for embedding in response.embeddings.ubinary] def take_action(state,query): resp = state["prev_comment"] if (resp == ""): return "営業支援でデータが生成されていません。" else: system_message = """あなたは、優秀なアシスタントです。""" message = f''' {resp}に基づいて{query}に対する答えを出力してください。金額の桁数は正確にしてください。 ''' # Add the system and user messages to the chat history messages = [ {"role": "system", "content": system_message}, {"role": "user", "content": message}, ] # Step 2: Tool planning and calling response = co.chat( model="command-r-plus-08-2024", messages=messages ) myresp = response.message.content[0].text return myresp def ma_action(query): system_message = """あなたは、優秀なアシスタントです。""" message = f''' {query}に対する答えを出力してください。金額の桁数は正確にしてください。 ''' # Add the system and user messages to the chat history messages = [ {"role": "system", "content": system_message}, {"role": "user", "content": message}, ] # Step 2: Tool planning and calling response = co.chat( model="command-r-plus-08-2024", messages=messages ) myresp = response.message.content[0].text return myresp def search_documents(state,query): # Embed the query query_embedding = embed([query], 'search_query')[0] # Convert numpy array to a Python list or tuple query_embedding = query_embedding.tolist() # Convert to list # Alternatively: query_embedding = tuple(query_embedding) # Execute SQL query cur.execute( 'SELECT salesID,salesManID,salesManName,CustomerID,CustomerName,Industry,Email,RegistrationDate,OpportunityDetails,OpportunityStage,ExpectedRevenue,ActivityType,ActivityDate,ActivityDetails FROM sales_statusdata ORDER BY datavector <=> %s::vector LIMIT 1', (query_embedding,) ) sales_data = "" # Fetch results data = cur.fetchall() sales_id = data[0][0] salesman_id = data[0][1] salesman_name = data[0][2] customer_id = data[0][3] customer_name = data[0][4] industry = data[0][5] email = data[0][6] registration_date = data[0][7] opportunity_details = data[0][8] opportunity_stage = data[0][9] expected_revenue = data[0][10] activity_type = data[0][11] activity_date = data[0][12] activity_details = data[0][13] sales_data = f"セールズID: {sales_id} 営業担当者ID: {salesman_id} 営業担当者名: {salesman_name} 顧客ID: {customer_id} 顧客名: {customer_name} 業界: {industry} 顧客メール: {email} 登録日: {registration_date} 獲得経緯: {opportunity_details} ステージ: {opportunity_stage} 予想売上: {expected_revenue} タイプ: {activity_type} 営業日付: {activity_date} 詳細: {activity_details}" print("sales_data:",sales_data) system_message = """あなたは、優秀なアシスタントです。""" message = f''' {sales_data}に基づいて{query}に対する答えを出力してください。金額の桁数は正確にしてください。 ''' # Add the system and user messages to the chat history messages = [ {"role": "system", "content": system_message}, {"role": "user", "content": message}, ] # Step 2: Tool planning and calling response = co.chat( model="command-r-plus-08-2024", messages=messages ) myresp = response.message.content[0].text state["prev_comment"] = myresp return myresp # Setting up Gradio Blocks with gr.Blocks(css="footer {visibility: hidden;} #custom-btn {width: 150px; height: 30px; padding-left: 0px; margin-left: auto; margin-left: auto; text-align: right; background-color: transparent; border-radius: 10px; font-size: 12px; color: #3C82F6;} #header {display: flex; justify-content: space-between; align-items: center; font-size: 24px; font-weight: bold;} #logo {width: 50px; height: 50px;}",title="営業支援エージェント") as app: gr.HTML('
