Create functions.py
Browse files- functions.py +135 -0
functions.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import requests
|
| 3 |
+
import smtplib
|
| 4 |
+
from email.mime.text import MIMEText
|
| 5 |
+
from email.mime.multipart import MIMEMultipart
|
| 6 |
+
from duckduckgo_search import DDGS
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
emailkey = os.getenv("EMAIL_KEY")
|
| 10 |
+
|
| 11 |
+
FUNCTIONS = [
|
| 12 |
+
{
|
| 13 |
+
"name": "search_duckduckgo",
|
| 14 |
+
"description": "使用DuckDuckGo搜索引擎查询信息。可以搜索最新新闻、文章、博客等内容。",
|
| 15 |
+
"parameters": {
|
| 16 |
+
"type": "object",
|
| 17 |
+
"properties": {
|
| 18 |
+
"keywords": {
|
| 19 |
+
"type": "array",
|
| 20 |
+
"items": {"type": "string"},
|
| 21 |
+
"description": "搜索的关键词列表。例如:['Python', '机器学习', '最新进展']。"
|
| 22 |
+
}
|
| 23 |
+
},
|
| 24 |
+
"required": ["keywords"]
|
| 25 |
+
}
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"name": "search_papers",
|
| 29 |
+
"description": "使用Crossref API搜索学术论文。",
|
| 30 |
+
"parameters": {
|
| 31 |
+
"type": "object",
|
| 32 |
+
"properties": {
|
| 33 |
+
"query": {
|
| 34 |
+
"type": "string",
|
| 35 |
+
"description": "搜索查询字符串。例如:'climate change'。"
|
| 36 |
+
}
|
| 37 |
+
},
|
| 38 |
+
"required": ["query"]
|
| 39 |
+
}
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"name": "send_email",
|
| 43 |
+
"description": "发送电子邮件。",
|
| 44 |
+
"parameters": {
|
| 45 |
+
"type": "object",
|
| 46 |
+
"properties": {
|
| 47 |
+
"to": {
|
| 48 |
+
"type": "string",
|
| 49 |
+
"description": "收件人邮箱地址"
|
| 50 |
+
},
|
| 51 |
+
"subject": {
|
| 52 |
+
"type": "string",
|
| 53 |
+
"description": "邮件主题"
|
| 54 |
+
},
|
| 55 |
+
"content": {
|
| 56 |
+
"type": "string",
|
| 57 |
+
"description": "邮件内容"
|
| 58 |
+
}
|
| 59 |
+
},
|
| 60 |
+
"required": ["to", "subject", "content"]
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
FUNCTIONS_GROUP_1 = [FUNCTIONS[0], FUNCTIONS[1]] # search_duckduckgo, search_papers
|
| 66 |
+
FUNCTIONS_GROUP_2 = [FUNCTIONS[2]] # send_email
|
| 67 |
+
|
| 68 |
+
def search_duckduckgo(keywords):
|
| 69 |
+
search_term = " ".join(keywords)
|
| 70 |
+
with DDGS() as ddgs:
|
| 71 |
+
results = list(ddgs.text(keywords=search_term, region="cn-zh", safesearch="on", max_results=5))
|
| 72 |
+
return [{"title": result['title'], "body": result['body'].replace('\n', ' ')} for result in results]
|
| 73 |
+
|
| 74 |
+
def search_papers(query):
|
| 75 |
+
url = f"https://api.crossref.org/works?query={query}"
|
| 76 |
+
response = requests.get(url)
|
| 77 |
+
if response.status_code == 200:
|
| 78 |
+
data = response.json()
|
| 79 |
+
papers = data['message']['items']
|
| 80 |
+
processed_papers = []
|
| 81 |
+
for paper in papers:
|
| 82 |
+
processed_paper = {
|
| 83 |
+
"标题": paper.get('title', [''])[0],
|
| 84 |
+
"作者": ", ".join([f"{author.get('given', '')} {author.get('family', '')}" for author in paper.get('author', [])]),
|
| 85 |
+
"DOI": paper.get('DOI', ''),
|
| 86 |
+
"摘要": paper.get('abstract', '').replace('<p>', '').replace('</p>', '').replace('<italic>', '*').replace('</italic>', '*')
|
| 87 |
+
}
|
| 88 |
+
processed_papers.append(processed_paper)
|
| 89 |
+
return processed_papers
|
| 90 |
+
else:
|
| 91 |
+
return []
|
| 92 |
+
|
| 93 |
+
def send_email(to, subject, content):
|
| 94 |
+
try:
|
| 95 |
+
with smtplib.SMTP('106.15.184.28', 8025) as smtp:
|
| 96 |
+
smtp.login("jwt", emailkey)
|
| 97 |
+
message = MIMEMultipart()
|
| 98 |
+
message['From'] = "Me <aixiao@aixiao.xyz>"
|
| 99 |
+
message['To'] = to
|
| 100 |
+
message['Subject'] = subject
|
| 101 |
+
message.attach(MIMEText(content, 'html'))
|
| 102 |
+
smtp.sendmail("aixiao@aixiao.xyz", to, message.as_string())
|
| 103 |
+
return True
|
| 104 |
+
except Exception as e:
|
| 105 |
+
print(f"发送邮件时出错: {str(e)}")
|
| 106 |
+
return False
|
| 107 |
+
|
| 108 |
+
def process_function_call(function_name, function_args):
|
| 109 |
+
if function_name == "search_duckduckgo":
|
| 110 |
+
keywords = function_args.get('keywords', [])
|
| 111 |
+
if not keywords:
|
| 112 |
+
return "搜索关键词为空,无法执行搜索。"
|
| 113 |
+
return search_duckduckgo(keywords)
|
| 114 |
+
elif function_name == "search_papers":
|
| 115 |
+
query = function_args.get('query', '')
|
| 116 |
+
if not query:
|
| 117 |
+
return "搜索查询为空,无法执行论文搜索。"
|
| 118 |
+
return search_papers(query)
|
| 119 |
+
elif function_name == "send_email":
|
| 120 |
+
to = function_args.get('to', '')
|
| 121 |
+
subject = function_args.get('subject', '')
|
| 122 |
+
content = function_args.get('content', '')
|
| 123 |
+
if not to or not subject or not content:
|
| 124 |
+
return "邮件信息不完整,无法发送邮件。"
|
| 125 |
+
success = send_email(to, subject, content)
|
| 126 |
+
return {
|
| 127 |
+
"success": success,
|
| 128 |
+
"message": "邮件发送成功" if success else "邮件发送失败",
|
| 129 |
+
"to": to,
|
| 130 |
+
"subject": subject,
|
| 131 |
+
"content": content,
|
| 132 |
+
"is_email": True
|
| 133 |
+
}
|
| 134 |
+
else:
|
| 135 |
+
return "未知的函数调用。"
|