Spaces:
Runtime error
Runtime error
Update tavily_search.py
Browse files- tavily_search.py +19 -61
tavily_search.py
CHANGED
|
@@ -1,18 +1,17 @@
|
|
| 1 |
import requests
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
-
import openai
|
| 5 |
-
import time
|
| 6 |
from urllib.parse import urlparse
|
| 7 |
from requests.adapters import HTTPAdapter
|
| 8 |
from requests.packages.urllib3.util.retry import Retry
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Tavily APIの設定
|
| 11 |
TAVILY_API_URL = "https://api.tavily.com/search"
|
| 12 |
API_KEY = os.getenv('TAVILY_API_KEY')
|
| 13 |
-
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
| 14 |
-
|
| 15 |
-
openai.api_key = OPENAI_API_KEY
|
| 16 |
|
| 17 |
def filter_results_by_domain(results, allowed_domains):
|
| 18 |
filtered_results = []
|
|
@@ -24,12 +23,8 @@ def filter_results_by_domain(results, allowed_domains):
|
|
| 24 |
|
| 25 |
def search_tavily(query, domain_filter, retries=3, backoff_factor=1.0, timeout=30):
|
| 26 |
headers = {"Content-Type": "application/json"}
|
| 27 |
-
|
| 28 |
-
# キーワードが5文字以下の場合、デフォルトのフレーズを追加
|
| 29 |
if len(query) <= 5:
|
| 30 |
query += " example"
|
| 31 |
-
|
| 32 |
-
# 検索パラメータの定義
|
| 33 |
params = {
|
| 34 |
'api_key': API_KEY,
|
| 35 |
'query': query,
|
|
@@ -37,17 +32,10 @@ def search_tavily(query, domain_filter, retries=3, backoff_factor=1.0, timeout=3
|
|
| 37 |
'detail_level': 'high',
|
| 38 |
'search_depth': 'advanced'
|
| 39 |
}
|
| 40 |
-
|
| 41 |
-
retry_strategy = Retry(
|
| 42 |
-
total=retries,
|
| 43 |
-
status_forcelist=[429, 500, 502, 503, 504],
|
| 44 |
-
allowed_methods=["POST"],
|
| 45 |
-
backoff_factor=backoff_factor
|
| 46 |
-
)
|
| 47 |
adapter = HTTPAdapter(max_retries=retry_strategy)
|
| 48 |
http = requests.Session()
|
| 49 |
http.mount("https://", adapter)
|
| 50 |
-
|
| 51 |
for attempt in range(retries):
|
| 52 |
try:
|
| 53 |
response = http.post(TAVILY_API_URL, headers=headers, json=params, timeout=timeout)
|
|
@@ -60,60 +48,30 @@ def search_tavily(query, domain_filter, retries=3, backoff_factor=1.0, timeout=3
|
|
| 60 |
return {'success': True, 'results': results['results']}
|
| 61 |
elif response.status_code >= 500:
|
| 62 |
time.sleep(backoff_factor * (2 ** attempt))
|
| 63 |
-
else:
|
| 64 |
-
return {"success": False, "error": f"結果取得に失敗しました: {response.status_code}", 'results': []}
|
| 65 |
except requests.exceptions.RequestException as e:
|
| 66 |
time.sleep(backoff_factor * (2 ** attempt))
|
| 67 |
-
|
| 68 |
return {"success": False, "error": "結果取得に失敗しました。後でもう一度試してください。", 'results': []}
|
| 69 |
|
| 70 |
def summarize_results(results):
|
| 71 |
if not results.get('success', False):
|
| 72 |
return results.get('error', 'Unknown error occurred.')
|
| 73 |
-
|
| 74 |
result_items = results.get('results', [])
|
| 75 |
if not result_items:
|
| 76 |
return "Error: No results found or unexpected data format."
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
references = []
|
| 80 |
-
for index, result in enumerate(result_items, start=1):
|
| 81 |
-
if 'content' in result and 'url' in result:
|
| 82 |
-
snippets.append(f"{result['content']} [^{index}]")
|
| 83 |
-
references.append(f"[^{index}]: {result['url']}")
|
| 84 |
-
|
| 85 |
joined_snippets = "\n\n".join(snippets)
|
| 86 |
joined_references = "\n".join(references)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
f"テキストを生成する際に、参照した情報のURLを脚注やリンクとしてMarkdown形式で含めてください。\n\n{joined_snippets}\n\n{joined_references}"
|
| 98 |
-
)
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
-
try:
|
| 102 |
-
response = openai.ChatCompletion.create(
|
| 103 |
-
model="gpt-4o",
|
| 104 |
-
messages=[system_message, user_message],
|
| 105 |
-
temperature=0.7,
|
| 106 |
-
max_tokens=3000,
|
| 107 |
-
timeout=120
|
| 108 |
-
)
|
| 109 |
-
summary = response.choices[0]["message"]["content"].strip()
|
| 110 |
-
markdown_result = f"\n{summary}\n\n参照URL:\n{joined_references}"
|
| 111 |
-
html_result = markdown_result.replace("\n", "<br>").replace(" ", " ") # 簡単なHTML変換
|
| 112 |
-
return {'markdown': markdown_result, 'html': html_result}
|
| 113 |
-
except openai.error.OpenAIError as e:
|
| 114 |
-
return {"markdown": "Error: Unable to summarize the results.", "html": "Error: Unable to summarize the results."}
|
| 115 |
-
except requests.exceptions.Timeout as e:
|
| 116 |
-
return {"markdown": "Error: OpenAI API request timed out.", "html": "Error: OpenAI API request timed out."}
|
| 117 |
|
| 118 |
# Gradioインターフェースの設定
|
| 119 |
def tavily_search_interface(selected_text, domain_filter):
|
|
@@ -129,7 +87,7 @@ def create_tavily_search_ui():
|
|
| 129 |
search_button = gr.Button("検索", elem_classes="right-align")
|
| 130 |
search_results_md = gr.Markdown(elem_classes="right-align")
|
| 131 |
search_results_html = gr.HTML(elem_classes="right-align", visible=False)
|
| 132 |
-
|
| 133 |
search_button.click(tavily_search_interface, inputs=[selected_text, domain_filter], outputs=[search_results_md, search_results_html])
|
|
|
|
| 134 |
|
| 135 |
-
|
|
|
|
| 1 |
import requests
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
from urllib.parse import urlparse
|
| 5 |
from requests.adapters import HTTPAdapter
|
| 6 |
from requests.packages.urllib3.util.retry import Retry
|
| 7 |
+
from groq import Groq # Groqクライアントをインポート
|
| 8 |
+
|
| 9 |
+
# Groq APIの設定
|
| 10 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 11 |
|
| 12 |
# Tavily APIの設定
|
| 13 |
TAVILY_API_URL = "https://api.tavily.com/search"
|
| 14 |
API_KEY = os.getenv('TAVILY_API_KEY')
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def filter_results_by_domain(results, allowed_domains):
|
| 17 |
filtered_results = []
|
|
|
|
| 23 |
|
| 24 |
def search_tavily(query, domain_filter, retries=3, backoff_factor=1.0, timeout=30):
|
| 25 |
headers = {"Content-Type": "application/json"}
|
|
|
|
|
|
|
| 26 |
if len(query) <= 5:
|
| 27 |
query += " example"
|
|
|
|
|
|
|
| 28 |
params = {
|
| 29 |
'api_key': API_KEY,
|
| 30 |
'query': query,
|
|
|
|
| 32 |
'detail_level': 'high',
|
| 33 |
'search_depth': 'advanced'
|
| 34 |
}
|
| 35 |
+
retry_strategy = Retry(total=retries, status_forcelist=[429, 500, 502, 503, 504], allowed_methods=["POST"], backoff_factor=backoff_factor)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
adapter = HTTPAdapter(max_retries=retry_strategy)
|
| 37 |
http = requests.Session()
|
| 38 |
http.mount("https://", adapter)
|
|
|
|
| 39 |
for attempt in range(retries):
|
| 40 |
try:
|
| 41 |
response = http.post(TAVILY_API_URL, headers=headers, json=params, timeout=timeout)
|
|
|
|
| 48 |
return {'success': True, 'results': results['results']}
|
| 49 |
elif response.status_code >= 500:
|
| 50 |
time.sleep(backoff_factor * (2 ** attempt))
|
|
|
|
|
|
|
| 51 |
except requests.exceptions.RequestException as e:
|
| 52 |
time.sleep(backoff_factor * (2 ** attempt))
|
|
|
|
| 53 |
return {"success": False, "error": "結果取得に失敗しました。後でもう一度試してください。", 'results': []}
|
| 54 |
|
| 55 |
def summarize_results(results):
|
| 56 |
if not results.get('success', False):
|
| 57 |
return results.get('error', 'Unknown error occurred.')
|
|
|
|
| 58 |
result_items = results.get('results', [])
|
| 59 |
if not result_items:
|
| 60 |
return "Error: No results found or unexpected data format."
|
| 61 |
+
snippets = [f"{result['content']} [^{index}]" for index, result in enumerate(result_items, start=1)]
|
| 62 |
+
references = [f"[^{index}]: {result['url']}" for index, result in enumerate(result_items, start=1)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
joined_snippets = "\n\n".join(snippets)
|
| 64 |
joined_references = "\n".join(references)
|
| 65 |
+
chat_completion = client.chat.completions.create(
|
| 66 |
+
messages=[
|
| 67 |
+
{"role": "user", "content": "Translate the following text to Japanese:\n\n" + joined_snippets + "\n\n" + joined_references}
|
| 68 |
+
],
|
| 69 |
+
model="llama3-8b-8192",
|
| 70 |
+
temperature=0.5 # 仮にtemperatureを設定できる場合
|
| 71 |
+
)
|
| 72 |
+
markdown_result = f"\n{chat_completion.choices[0].message.content}\n\n参照URL:\n{joined_references}"
|
| 73 |
+
html_result = markdown_result.replace("\n", "<br>").replace(" ", " ")
|
| 74 |
+
return {'markdown': markdown_result, 'html': html_result}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# Gradioインターフェースの設定
|
| 77 |
def tavily_search_interface(selected_text, domain_filter):
|
|
|
|
| 87 |
search_button = gr.Button("検索", elem_classes="right-align")
|
| 88 |
search_results_md = gr.Markdown(elem_classes="right-align")
|
| 89 |
search_results_html = gr.HTML(elem_classes="right-align", visible=False)
|
|
|
|
| 90 |
search_button.click(tavily_search_interface, inputs=[selected_text, domain_filter], outputs=[search_results_md, search_results_html])
|
| 91 |
+
return demo
|
| 92 |
|
| 93 |
+
create_tavily_search_ui().launch()
|