| import gradio as gr |
| from gradio import ChatMessage |
| |
| from groq import Groq |
| import requests |
| import uvicorn |
| import os |
| import mysql.connector |
| from qdrant_client import QdrantClient |
|
|
| from autogen import AssistantAgent, UserProxyAgent, GroupChat, GroupChatManager |
| from autogen.agentchat.contrib.retrieve_user_proxy_agent import RetrieveUserProxyAgent |
|
|
| |
| conn = mysql.connector.connect( |
| host="www.ryhintl.com", |
| user="smairuser", |
| password="smairuser", |
| port=36000, |
| database="smair" |
| ) |
|
|
| |
| cursor = conn.cursor(dictionary=True) |
|
|
| |
| select_one_data_query = "SELECT * FROM agentic_apis" |
| cursor.execute(select_one_data_query) |
| result = cursor.fetchall() |
| |
| keys = [item['key'] for item in result] |
|
|
| os.environ["GROQ_API_KEY"] = keys[2] |
| os.environ["COHERE_API_KEY"] = keys[3] |
|
|
| 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 = 'プロジェクト・マッチング候補者のガイドライン'; |
| 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); |
| }, i * 250); |
| })(i); |
| } |
| |
| var gradioContainer = document.querySelector('.gradio-container'); |
| gradioContainer.insertBefore(container, gradioContainer.firstChild); |
| |
| return 'Animation created'; |
| } |
| """ |
|
|
| sel_method = "COHERE" |
|
|
| def on_change(selected_option): |
| global sel_method |
| sel_method = selected_option |
| return f"選択されたオプション: {selected_option}" |
|
|
| async def autogen(qry: str): |
| |
| userPrompt=qry |
| assistant="あなたはヘッドハンティング会社のアナリストです。" |
| ragproxyagent="あなたは高度な知識を持つヘッドハンティング会社の上席コンサルタントです。assistantによる応答を分析し、詳細情報を提供します。" |
|
|
| global sel_method |
| |
| if sel_method == "COHERE": |
| |
| config_list = [ |
| { |
| "model": "command-r-plus-08-2024", |
| "api_key": os.environ["COHERE_API_KEY"], |
| "api_type": "cohere", |
| } |
| ] |
| else: |
| |
| config_list = [ |
| { |
| "model": "llama-3.3-70b-versatile", |
| "api_key": os.environ["GROQ_API_KEY"], |
| "api_type": "groq", |
| } |
| ] |
|
|
| assistant = AssistantAgent( |
| name="assistant", |
| system_message="You are a helpful assistant.", |
| llm_config={ |
| "timeout": 600, |
| "cache_seed": 42, |
| "config_list": config_list, |
| }, |
| ) |
|
|
| client = QdrantClient(url="https://02cbe366-829e-43a6-adf5-3b712a886c21.us-west-1-0.aws.cloud.qdrant.io", api_key="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2Nlc3MiOiJtIn0.hWu5_qKaYHUhuMAjUScqw1R_1kkXiidv337wuGKcu9o") |
| |
| ragproxyagent = RetrieveUserProxyAgent( |
| name="ragproxyagent", |
| human_input_mode="NEVER", |
| max_consecutive_auto_reply=3, |
| retrieve_config = { |
| "task": "qa", |
| "docs_path": [ |
| "https://www.ryhintl.com/hunting.md" |
| ], |
| "chunk_token_size": 2000, |
| "model": config_list[0]["model"], |
| "vector_db": "qdrant", |
| "collection_name": "scouter_base", |
| |
| |
| |
| |
| |
| |
| |
| |
| "db_config": {"client": client}, |
| "get_or_create": True, |
| "overwrite": True, |
| }, |
| code_execution_config=False, |
| ) |
|
|
|
|
| |
| groupChat = GroupChat( |
| agents=[assistant, ragproxyagent], |
| messages=[], |
| max_round=3, |
| ) |
|
|
| groupChatManager = GroupChatManager( |
| groupchat=groupChat, |
| llm_config={"config_list": config_list} |
| ) |
|
|
| |
| |
| |
| |
| |
| chat_result = ragproxyagent.initiate_chat(assistant, message=ragproxyagent.message_generator, problem=userPrompt) |
| |
| |
| |
| |
|
|
| chat_result_dict = vars(chat_result) |
| user_contents = [entry['content'] for entry in chat_result_dict['chat_history'] if entry['role'] == 'user'] |
| |
| |
| |
| |
| |
| |
|
|
| basic_content = [entry["content"] for entry in chat_result_dict["chat_history"] if entry["role"] == "assistant"] |
| |
| |
| |
| return user_contents[0]+"\n\n"+basic_content[0] |
|
|
| |
| |
|
|
| |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| def process_rag(prompt): |
|
|
| if prompt == "": |
| return "プロンプトを入力してください。", "プロンプトは必須です。" |
| else: |
| url = 'http://www.ryhintl.com/eprag-be/llm?query='+prompt |
| res = requests.get(url) |
| rtn = res.content.decode('utf-8') |
| |
| return rtn |
|
|
|
|
| def process_eprag(prompt): |
| |
| if prompt == "": |
| return "プロンプトを入力してください。", "プロンプトは必須です。" |
| else: |
| client = Groq(api_key=os.environ["GROQ_API_KEY"]) |
|
|
| |
| system_prompt = { |
| "role": "system", |
| "content": "あなたは便利なアシスタントです。質問には簡潔に答えてください。" |
| } |
|
|
| |
| user_input = prompt |
| user_prompt = { |
| "role": "user", "content": user_input |
| } |
|
|
| |
| 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) |
|
|
| return response.choices[0].message.content |
|
|
| with gr.Blocks(js=js,title="人材スカウトのガイドライン") as llm: |
| |
| with gr.Tab("LLM"): |
| |
| gr.Markdown("# 🗞️ LLM") |
| gr.Text(label="AIプロジェクト計画の実施の人材スカウト",value="弊社(スカウトファーム)は株式会社トータルソリューションズのAIプロジェクトの立ち上げに必要な専門知識を持っている人材が不足して悩んでいます。AIに関する専門知識を持っている専門家とPYTHONやAGENTの経験を持っている技術者を候補者リストDBから取得、分析し、AIプロジェクトの立ち上げをするための候補者をアドバイスします。",lines=3) |
| |
| with gr.Row(): |
| eprag_input = gr.Textbox(value="トータルソリューションズのAIプロジェクトの立ち上げに必要な専門知識を持っている人材が不足して悩んでいます。AIに関する専門知識を持っている専門家とPYTHONやAGENTの経験を持っている技術者をリスティングして、アドバイスしてください。",lines=5,label="プロンプト", type="text") |
| |
| with gr.Row(): |
| eprag_output = gr.Textbox(label="AIアシスタントの応答") |
| |
| submit_button = gr.Button("LLM プロセス", variant="primary") |
| submit_button.click( |
| process_eprag, |
| inputs=[eprag_input], |
| outputs=[eprag_output] |
| ) |
|
|
| with gr.Tab("RAG"): |
| |
| gr.Markdown("# 🗞️ RAG") |
| gr.Text(label="AIプロジェクト計画の実施の人材スカウト",value="弊社(スカウトファーム)は株式会社トータルソリューションズのAIプロジェクトの立ち上げに必要な専門知識を持っている人材が不足して悩んでいます。AIに関する専門知識を持っている専門家とPYTHONやAGENTの経験を持っている技術者を候補者リストDBから取得、分析し、AIプロジェクトの立ち上げをするための候補者をアドバイスします。",lines=3) |
| |
| with gr.Row(): |
| rag_input = gr.Textbox(value="トータルソリューションズのAIプロジェクトの立ち上げに必要な専門知識を持っている人材が不足して悩んでいます。AIに関する専門知識を持っている専門家とPYTHONやAGENTの経験を持っている技術者をリスティングして、アドバイスしてください。",lines=5,label="プロンプト", type="text") |
| |
| with gr.Row(): |
| rag_output = gr.Textbox(label="AIアシスタントの応答") |
| |
| submit_button = gr.Button("RAG プロセス", variant="primary") |
| submit_button.click( |
| process_rag, |
| inputs=[rag_input], |
| outputs=[rag_output] |
| ) |
|
|
| with gr.Tab("AGENT AUTOGEN"): |
| gr.Markdown("# 🗞️ AGENTIC RAG AUTOGEN") |
| gr.Text(label="AIプロジェクト計画の実施の人材スカウト",value="弊社(スカウトファーム)は株式会社トータルソリューションズのAIプロジェクトの立ち上げに必要な専門知識を持っている人材が不足して悩んでいます。AIに関する専門知識を持っている専門家とPYTHONやAGENTの経験を持っている技術者を候補者リストDBから取得、分析し、AIプロジェクトの立ち上げをするための候補者をアドバイスします。",lines=3) |
| |
| with gr.Row(): |
| auto_input = gr.Textbox(value="トータルソリューションズのAIプロジェクトの立ち上げに必要な専門知識を持っている人材が不足して悩んでいます。AIに関する専門知識を持っている専門家とPYTHONやAGENTの経験を持っている技術者をリスティングして、アドバイスしてください。",lines=5,label="プロンプト", type="text") |
| |
| with gr.Row(): |
| method = gr.Dropdown( |
| ["COHERE", "GROQ"], label="LLM", info="LLMを選んでください" |
| ) |
| selected_method = gr.Text(label="Method", visible=False) |
| method.change(on_change, inputs=method, outputs=selected_method) |
| |
| with gr.Row(): |
| auto_output = gr.Textbox(label="AIアシスタントの応答") |
| |
| submit_button = gr.Button("AUTOGEN プロセス", variant="primary") |
| submit_button.click( |
| autogen, |
| inputs=[auto_input], |
| outputs=[auto_output] |
| ) |
|
|
| if __name__ == "__main__": |
| llm.launch() |