beswarm 0.2.50__py3-none-any.whl → 0.2.52__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of beswarm might be problematic. Click here for more details.

Files changed (86) hide show
  1. beswarm/agents/chatgroup.py +2 -2
  2. beswarm/agents/planact.py +2 -2
  3. beswarm/aient/{src/aient → aient}/core/response.py +28 -38
  4. beswarm/aient/{src/aient → aient}/core/utils.py +8 -2
  5. beswarm/aient/aient/models/__init__.py +2 -0
  6. beswarm/aient/{src/aient → aient}/models/base.py +31 -15
  7. beswarm/aient/{src/aient → aient}/models/chatgpt.py +119 -186
  8. beswarm/aient/{src/aient → aient}/plugins/excute_command.py +1 -1
  9. beswarm/aient/{src/aient → aient}/plugins/read_file.py +1 -1
  10. beswarm/aient/{src/aient → aient}/plugins/websearch.py +1 -1
  11. beswarm/aient/{src/aient → aient}/plugins/write_file.py +1 -1
  12. beswarm/aient/{src/aient → aient}/utils/scripts.py +3 -1
  13. beswarm/knowledge_graph.py +47 -2
  14. beswarm/taskmanager.py +1 -1
  15. beswarm/tools/__init__.py +1 -1
  16. beswarm/tools/click.py +3 -3
  17. beswarm/tools/completion.py +1 -1
  18. beswarm/tools/edit_file.py +2 -2
  19. beswarm/tools/graph.py +1 -1
  20. beswarm/tools/planner.py +3 -3
  21. beswarm/tools/repomap.py +1 -1
  22. beswarm/tools/request_input.py +1 -1
  23. beswarm/tools/screenshot.py +1 -1
  24. beswarm/tools/search_arxiv.py +1 -1
  25. beswarm/tools/search_web.py +1 -1
  26. beswarm/tools/subtasks.py +1 -1
  27. beswarm/tools/worker.py +1 -1
  28. beswarm/tools/write_csv.py +1 -1
  29. beswarm/utils.py +1 -1
  30. {beswarm-0.2.50.dist-info → beswarm-0.2.52.dist-info}/METADATA +1 -1
  31. beswarm-0.2.52.dist-info/RECORD +122 -0
  32. beswarm/aient/main.py +0 -50
  33. beswarm/aient/setup.py +0 -15
  34. beswarm/aient/src/aient/models/__init__.py +0 -9
  35. beswarm/aient/src/aient/models/claude.py +0 -573
  36. beswarm/aient/src/aient/models/duckduckgo.py +0 -241
  37. beswarm/aient/src/aient/models/gemini.py +0 -357
  38. beswarm/aient/src/aient/models/groq.py +0 -234
  39. beswarm/aient/src/aient/models/vertex.py +0 -420
  40. beswarm/aient/test/chatgpt.py +0 -161
  41. beswarm/aient/test/claude.py +0 -32
  42. beswarm/aient/test/test.py +0 -2
  43. beswarm/aient/test/test_API.py +0 -6
  44. beswarm/aient/test/test_Deepbricks.py +0 -20
  45. beswarm/aient/test/test_aiwaves.py +0 -25
  46. beswarm/aient/test/test_aiwaves_arxiv.py +0 -19
  47. beswarm/aient/test/test_ask_gemini.py +0 -8
  48. beswarm/aient/test/test_class.py +0 -17
  49. beswarm/aient/test/test_claude.py +0 -23
  50. beswarm/aient/test/test_claude_zh_char.py +0 -26
  51. beswarm/aient/test/test_download_pdf.py +0 -56
  52. beswarm/aient/test/test_gemini.py +0 -97
  53. beswarm/aient/test/test_get_token_dict.py +0 -21
  54. beswarm/aient/test/test_jieba.py +0 -32
  55. beswarm/aient/test/test_json.py +0 -65
  56. beswarm/aient/test/test_logging.py +0 -32
  57. beswarm/aient/test/test_py_run.py +0 -26
  58. beswarm/aient/test/test_requests.py +0 -162
  59. beswarm/aient/test/test_tikitoken.py +0 -19
  60. beswarm/aient/test/test_token.py +0 -94
  61. beswarm/aient/test/test_wildcard.py +0 -20
  62. beswarm-0.2.50.dist-info/RECORD +0 -151
  63. /beswarm/aient/{src/aient → aient}/__init__.py +0 -0
  64. /beswarm/aient/{src/aient → aient}/core/__init__.py +0 -0
  65. /beswarm/aient/{src/aient → aient}/core/log_config.py +0 -0
  66. /beswarm/aient/{src/aient → aient}/core/models.py +0 -0
  67. /beswarm/aient/{src/aient → aient}/core/request.py +0 -0
  68. /beswarm/aient/{src/aient → aient}/core/test/test_base_api.py +0 -0
  69. /beswarm/aient/{src/aient → aient}/core/test/test_geminimask.py +0 -0
  70. /beswarm/aient/{src/aient → aient}/core/test/test_image.py +0 -0
  71. /beswarm/aient/{src/aient → aient}/core/test/test_payload.py +0 -0
  72. /beswarm/aient/{src/aient → aient}/models/audio.py +0 -0
  73. /beswarm/aient/{src/aient → aient}/plugins/__init__.py +0 -0
  74. /beswarm/aient/{src/aient → aient}/plugins/arXiv.py +0 -0
  75. /beswarm/aient/{src/aient → aient}/plugins/config.py +0 -0
  76. /beswarm/aient/{src/aient → aient}/plugins/get_time.py +0 -0
  77. /beswarm/aient/{src/aient → aient}/plugins/image.py +0 -0
  78. /beswarm/aient/{src/aient → aient}/plugins/list_directory.py +0 -0
  79. /beswarm/aient/{src/aient → aient}/plugins/read_image.py +0 -0
  80. /beswarm/aient/{src/aient → aient}/plugins/readonly.py +0 -0
  81. /beswarm/aient/{src/aient → aient}/plugins/registry.py +0 -0
  82. /beswarm/aient/{src/aient → aient}/plugins/run_python.py +0 -0
  83. /beswarm/aient/{src/aient → aient}/utils/__init__.py +0 -0
  84. /beswarm/aient/{src/aient → aient}/utils/prompt.py +0 -0
  85. {beswarm-0.2.50.dist-info → beswarm-0.2.52.dist-info}/WHEEL +0 -0
  86. {beswarm-0.2.50.dist-info → beswarm-0.2.52.dist-info}/top_level.txt +0 -0
@@ -1,161 +0,0 @@
1
- function_call_list = \
2
- {
3
- "base": {
4
- "tools": [],
5
- "tool_choice": "auto"
6
- },
7
- "current_weather": {
8
- "name": "get_current_weather",
9
- "description": "Get the current weather in a given location",
10
- "parameters": {
11
- "type": "object",
12
- "properties": {
13
- "location": {
14
- "type": "string",
15
- "description": "The city and state, e.g. San Francisco, CA"
16
- },
17
- "unit": {
18
- "type": "string",
19
- "enum": [
20
- "celsius",
21
- "fahrenheit"
22
- ]
23
- }
24
- },
25
- "required": [
26
- "location"
27
- ]
28
- }
29
- },
30
- "SEARCH": {
31
- "name": "get_search_results",
32
- "description": "Search Google to enhance knowledge.",
33
- "parameters": {
34
- "type": "object",
35
- "properties": {
36
- "prompt": {
37
- "type": "string",
38
- "description": "The prompt to search."
39
- }
40
- },
41
- "required": [
42
- "prompt"
43
- ]
44
- }
45
- },
46
- "URL": {
47
- "name": "get_url_content",
48
- "description": "Get the webpage content of a URL",
49
- "parameters": {
50
- "type": "object",
51
- "properties": {
52
- "url": {
53
- "type": "string",
54
- "description": "the URL to request"
55
- }
56
- },
57
- "required": [
58
- "url"
59
- ]
60
- }
61
- },
62
- "DATE": {
63
- "name": "get_time",
64
- "description": "Get the current time, date, and day of the week",
65
- },
66
- "VERSION": {
67
- "name": "get_version_info",
68
- "description": "Get version information",
69
- },
70
- "TARVEL": {
71
- "name": "get_city_tarvel_info",
72
- "description": "Get the city's travel plan by city name.",
73
- "parameters": {
74
- "type": "object",
75
- "properties": {
76
- "city": {
77
- "type": "string",
78
- "description": "the city to search"
79
- }
80
- },
81
- "required": [
82
- "city"
83
- ]
84
- }
85
- },
86
- "IMAGE": {
87
- "name": "generate_image",
88
- "description": "Generate images based on user descriptions.",
89
- "parameters": {
90
- "type": "object",
91
- "properties": {
92
- "prompt": {
93
- "type": "string",
94
- "description": "the prompt to generate image"
95
- }
96
- },
97
- "required": [
98
- "prompt"
99
- ]
100
- }
101
- },
102
- "CODE": {
103
- "name": "run_python_script",
104
- "description": "Convert the string to a Python script and return the Python execution result. Assign the result to the variable result. The results must be printed to the console using the print function. Directly output the code, without using quotation marks or other symbols to enclose the code.",
105
- "parameters": {
106
- "type": "object",
107
- "properties": {
108
- "prompt": {
109
- "type": "string",
110
- "description": "the code to run"
111
- }
112
- },
113
- "required": [
114
- "prompt"
115
- ]
116
- }
117
- },
118
- "ARXIV": {
119
- "name": "download_read_arxiv_pdf",
120
- "description": "Get the content of the paper corresponding to the arXiv ID",
121
- "parameters": {
122
- "type": "object",
123
- "properties": {
124
- "prompt": {
125
- "type": "string",
126
- "description": "the arXiv ID of the paper"
127
- }
128
- },
129
- "required": [
130
- "prompt"
131
- ]
132
- }
133
- },
134
- "FLIGHT": {
135
- "name": "get_Round_trip_flight_price",
136
- "description": "Get round-trip ticket prices between two cities for the next six months. Use two city names as parameters. The name of the citys must be in Chinese.",
137
- "parameters": {
138
- "type": "object",
139
- "properties": {
140
- "departcity": {
141
- "type": "string",
142
- "description": "the chinese name of departure city. e.g. 上海"
143
- },
144
- "arrivalcity": {
145
- "type": "string",
146
- "description": "the chinese name of arrival city. e.g. 北京"
147
- }
148
- },
149
- "required": [
150
- "departcity",
151
- "arrivalcity"
152
- ]
153
- }
154
- },
155
- }
156
-
157
-
158
- if __name__ == "__main__":
159
- import json
160
- tools_list = {"tools": [{"type": "function", "function": function_call_list[key]} for key in function_call_list.keys() if key != "base"]}
161
- print(json.dumps(tools_list, indent=4, ensure_ascii=False))
@@ -1,32 +0,0 @@
1
- # import os
2
- # import sys
3
- # print(os.path.dirname(os.path.abspath(__file__)))
4
- # sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
5
- from .chatgpt import function_call_list
6
- def gpt2claude_tools_json(json_dict):
7
- import copy
8
- json_dict = copy.deepcopy(json_dict)
9
- keys_to_change = {
10
- "parameters": "input_schema",
11
- }
12
- for old_key, new_key in keys_to_change.items():
13
- if old_key in json_dict:
14
- if new_key:
15
- json_dict[new_key] = json_dict.pop(old_key)
16
- else:
17
- json_dict.pop(old_key)
18
- else:
19
- if new_key and "description" in json_dict.keys():
20
- json_dict[new_key] = {
21
- "type": "object",
22
- "properties": {}
23
- }
24
- if "tools" in json_dict.keys():
25
- json_dict["tool_choice"] = {
26
- "type": "auto"
27
- }
28
- return json_dict
29
-
30
- claude_tools_list = {f"{key}": gpt2claude_tools_json(function_call_list[key]) for key in function_call_list.keys()}
31
- if __name__ == "__main__":
32
- print(claude_tools_list)
@@ -1,2 +0,0 @@
1
- a = "v1"
2
- print(a.split("v1"))
@@ -1,6 +0,0 @@
1
- def replace_with_asterisk(string, start=15, end=40):
2
- return string[:start] + '*' * (end - start) + string[end:]
3
-
4
- original_string = "sk-bvgiugvigycycyrfctdyxdxrts"
5
- result = replace_with_asterisk(original_string)
6
- print(result)
@@ -1,20 +0,0 @@
1
- import os
2
- from datetime import datetime
3
-
4
- from aient.models import chatgpt
5
- from aient.utils import prompt
6
-
7
- API = os.environ.get('API', None)
8
- API_URL = os.environ.get('API_URL', None)
9
- GPT_ENGINE = os.environ.get('GPT_ENGINE', 'gpt-4o')
10
- LANGUAGE = os.environ.get('LANGUAGE', 'Simplified Chinese')
11
-
12
- current_date = datetime.now()
13
- Current_Date = current_date.strftime("%Y-%m-%d")
14
-
15
- systemprompt = os.environ.get('SYSTEMPROMPT', prompt.system_prompt.format(LANGUAGE, Current_Date))
16
-
17
- bot = chatgpt(api_key=API, api_url=API_URL, engine=GPT_ENGINE, system_prompt=systemprompt)
18
- # for text in bot.ask_stream("你好"):
19
- for text in bot.ask_stream("arXiv:2311.17132 讲了什么?"):
20
- print(text, end="")
@@ -1,25 +0,0 @@
1
- import os
2
- from aient.models import chatgpt
3
-
4
- API = os.environ.get('API', None)
5
- API_URL = os.environ.get('API_URL', None)
6
- GPT_ENGINE = os.environ.get('GPT_ENGINE', 'gpt-4o')
7
-
8
- systemprompt = (
9
- "你是一位旅行规划专家。你需要帮助用户规划旅行行程,给出合理的行程安排。"
10
- "- 如果用户提及要从一个城市前往另外一个城市,必须使用 get_Round_trip_flight_price 查询两个城市半年内往返机票价格信息。"
11
- "- 在规划行程之前,必须使用 get_city_tarvel_info 查询城市的景点旅行攻略信息。"
12
- "- 查询攻略后,你需要分析用户个性化需求。充分考虑用户的年龄,情侣,家庭,朋友,儿童,独自旅行等情况。排除不适合用户个性化需求的景点。之后输出符合用户需求的景点。"
13
- "- 综合用户游玩时间,适合用户个性化需求的旅游城市景点,机票信息和预算,给出真实准确的旅游行程,包括游玩时长、景点之间的交通方式和移动距离,每天都要给出总的游玩时间。"
14
- "- 根据查到的景点介绍结合你自己的知识,每个景点必须包含你推荐的理由和景点介绍。介绍景点用户游玩的景点,景点介绍尽量丰富精彩,吸引用户眼球,不要直接复述查到的景点介绍。"
15
- "- 每个景点都要标注游玩时间、景点之间的交通方式和移动距离还有生动的景点介绍"
16
- "- 尽量排满用户的行程,不要有太多空闲时间。"
17
- )
18
- bot = chatgpt(api_key=API, api_url=API_URL, engine=GPT_ENGINE, system_prompt=systemprompt)
19
- for text in bot.ask_stream("我在上海想去重庆旅游,我只有2000元预算,我想在重庆玩一周,你能帮我规划一下吗?"):
20
- # for text in bot.ask_stream("我在广州市,想周一去香港,周四早上回来,是去游玩,请你帮我规划整个行程。包括细节,如交通,住宿,餐饮,价格,等等,最好细节到每天各个部分的时间,花费,等等,尽量具体,用户一看就能直接执行的那种"):
21
- # for text in bot.ask_stream("上海有哪些好玩的地方?"):
22
- # for text in bot.ask_stream("just say test"):
23
- # for text in bot.ask_stream("我在上海想去重庆旅游,我只有2000元预算,我想在重庆玩一周,你能帮我规划一下吗?"):
24
- # for text in bot.ask_stream("我在上海想去重庆旅游,我有一天的时间。你能帮我规划一下吗?"):
25
- print(text, end="")
@@ -1,19 +0,0 @@
1
- import os
2
- from datetime import datetime
3
-
4
- from aient.models import chatgpt
5
- from aient.utils import prompt
6
-
7
- API = os.environ.get('API', None)
8
- API_URL = os.environ.get('API_URL', None)
9
- GPT_ENGINE = os.environ.get('GPT_ENGINE', 'gpt-4o')
10
- LANGUAGE = os.environ.get('LANGUAGE', 'Simplified Chinese')
11
-
12
- current_date = datetime.now()
13
- Current_Date = current_date.strftime("%Y-%m-%d")
14
-
15
- systemprompt = os.environ.get('SYSTEMPROMPT', prompt.system_prompt.format(LANGUAGE, Current_Date))
16
-
17
- bot = chatgpt(api_key=API, api_url=API_URL, engine=GPT_ENGINE, system_prompt=systemprompt)
18
- for text in bot.ask_stream("arXiv:2311.17132 讲了什么?"):
19
- print(text, end="")
@@ -1,8 +0,0 @@
1
- import os
2
- from aient.models import gemini
3
-
4
- GOOGLE_AI_API_KEY = os.environ.get('GOOGLE_AI_API_KEY', None)
5
-
6
- bot = gemini(api_key=GOOGLE_AI_API_KEY, engine='gemini-2.0-flash-exp')
7
- for text in bot.ask_stream("give me some example code of next.js to build a modern web site"):
8
- print(text, end="")
@@ -1,17 +0,0 @@
1
- # return e
2
- def j(e, f):
3
- e(f)
4
- # return e
5
- class a:
6
- def __init__(self) -> None:
7
- self.b = [1, 2, 3]
8
- def d(self, e):
9
- e.append(4)
10
- def c(self):
11
- j(self.d, self.b)
12
- return self.b
13
-
14
- k = a()
15
- print(k.b)
16
- print(k.c())
17
- print(k.b)
@@ -1,23 +0,0 @@
1
- import os
2
- from datetime import datetime
3
-
4
- from aient.models import chatgpt, claude3
5
- from aient.utils import prompt
6
-
7
- API = os.environ.get('API', None)
8
- CLAUDE_API = os.environ.get('claude_api_key', None)
9
- API_URL = os.environ.get('API_URL', None)
10
- GPT_ENGINE = os.environ.get('GPT_ENGINE', 'gpt-4o')
11
- LANGUAGE = os.environ.get('LANGUAGE', 'Simplified Chinese')
12
-
13
- current_date = datetime.now()
14
- Current_Date = current_date.strftime("%Y-%m-%d")
15
-
16
- systemprompt = os.environ.get('SYSTEMPROMPT', prompt.system_prompt.format(LANGUAGE, Current_Date))
17
-
18
- # bot = chatgpt(api_key=API, api_url=API_URL, engine=GPT_ENGINE, system_prompt=systemprompt)
19
- bot = claude3(api_key=CLAUDE_API, engine=GPT_ENGINE, system_prompt=systemprompt)
20
- for text in bot.ask_stream("arXiv:2210.10716 这篇文章讲了啥"):
21
- # for text in bot.ask_stream("今天的微博热搜有哪些?"):
22
- # for text in bot.ask_stream("你现在是什么版本?"):
23
- print(text, end="")
@@ -1,26 +0,0 @@
1
- def is_surrounded_by_chinese(text, index):
2
- left_char = text[index - 1]
3
- if 0 < index < len(text) - 1:
4
- right_char = text[index + 1]
5
- return '\u4e00' <= left_char <= '\u9fff' or '\u4e00' <= right_char <= '\u9fff'
6
- if index == len(text) - 1:
7
- return '\u4e00' <= left_char <= '\u9fff'
8
- return False
9
-
10
- def replace_char(string, index, new_char):
11
- return string[:index] + new_char + string[index+1:]
12
-
13
- def claude_replace(text):
14
- Punctuation_mapping = {",": ",", ":": ":", "!": "!", "?": "?", ";": ";"}
15
- key_list = list(Punctuation_mapping.keys())
16
- for i in range(len(text)):
17
- if is_surrounded_by_chinese(text, i) and (text[i] in key_list):
18
- text = replace_char(text, i, Punctuation_mapping[text[i]])
19
- return text
20
-
21
- text = '''
22
- 你好!我是一名人工智能助手,很高兴见到你。有什么我可以帮助你的吗?无论是日常问题还是专业领域,我都会尽我所能为你解答。让我们开始愉快的交流吧!'''
23
-
24
- if __name__ == '__main__':
25
- new_text = claude_replace(text)
26
- print(new_text)
@@ -1,56 +0,0 @@
1
- # import requests
2
- # import urllib.parse
3
- # import os
4
- # import sys
5
- # sys.path.append(os.getcwd())
6
- # import config
7
-
8
- # from langchain.chat_models import ChatOpenAI
9
- # from langchain.embeddings.openai import OpenAIEmbeddings
10
- # from langchain.vectorstores import Chroma
11
- # from langchain.text_splitter import CharacterTextSplitter
12
- # from langchain.document_loaders import UnstructuredPDFLoader
13
- # from langchain.chains import RetrievalQA
14
-
15
-
16
- # def get_doc_from_url(url):
17
- # filename = urllib.parse.unquote(url.split("/")[-1])
18
- # response = requests.get(url, stream=True)
19
- # with open(filename, 'wb') as f:
20
- # for chunk in response.iter_content(chunk_size=1024):
21
- # f.write(chunk)
22
- # return filename
23
-
24
- # def pdf_search(docurl, query_message, model="gpt-3.5-turbo"):
25
- # chatllm = ChatOpenAI(temperature=0.5, openai_api_base=config.API_URL.split("chat")[0], model_name=model, openai_api_key=os.environ.get('API', None))
26
- # embeddings = OpenAIEmbeddings(openai_api_base=config.API_URL.split("chat")[0], openai_api_key=os.environ.get('API', None))
27
- # filename = get_doc_from_url(docurl)
28
- # docpath = os.getcwd() + "/" + filename
29
- # loader = UnstructuredPDFLoader(docpath)
30
- # print(docpath)
31
- # documents = loader.load()
32
- # os.remove(docpath)
33
- # # 初始化加载器
34
- # text_splitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=25)
35
- # # 切割加载的 document
36
- # split_docs = text_splitter.split_documents(documents)
37
- # vector_store = Chroma.from_documents(split_docs, embeddings)
38
- # # 创建问答对象
39
- # qa = RetrievalQA.from_chain_type(llm=chatllm, chain_type="stuff", retriever=vector_store.as_retriever(),return_source_documents=True)
40
- # # 进行问答
41
- # result = qa({"query": query_message})
42
- # return result['result']
43
-
44
- # pdf_search("https://www.nsfc.gov.cn/csc/20345/22468/pdf/2001/%E5%86%BB%E7%BB%93%E8%A3%82%E9%9A%99%E7%A0%82%E5%B2%A9%E4%BD%8E%E5%91%A8%E5%BE%AA%E7%8E%AF%E5%8A%A8%E5%8A%9B%E7%89%B9%E6%80%A7%E8%AF%95%E9%AA%8C%E7%A0%94%E7%A9%B6.pdf", "端水实验的目的是什么?")
45
-
46
- from PyPDF2 import PdfReader
47
-
48
- def has_text(pdf_path):
49
- with open(pdf_path, 'rb') as file:
50
- pdf = PdfReader(file)
51
- page = pdf.pages[0]
52
- text = page.extract_text()
53
- return text
54
-
55
- pdf_path = '/Users/yanyuming/Downloads/GitHub/ChatGPT-Telegram-Bot/冻结裂隙砂岩低周循环动力特性试验研究.pdf'
56
- print(has_text(pdf_path))
@@ -1,97 +0,0 @@
1
- import json
2
-
3
- class JSONExtractor:
4
- def __init__(self):
5
- self.buffer = ""
6
- self.bracket_count = 0
7
- self.in_target = False
8
- self.target_json = ""
9
-
10
- def process_line(self, line):
11
- self.buffer += line.strip()
12
-
13
- for char in line:
14
- if char == '{':
15
- self.bracket_count += 1
16
- if self.bracket_count == 4 and '"functionCall"' in self.buffer[-20:]:
17
- self.in_target = True
18
- self.target_json = '{'
19
- elif char == '}':
20
- if self.in_target:
21
- self.target_json += '}'
22
- self.bracket_count -= 1
23
- if self.bracket_count == 3 and self.in_target:
24
- self.in_target = False
25
- return self.parse_target_json()
26
-
27
- if self.in_target:
28
- self.target_json += char
29
-
30
- return None
31
-
32
- def parse_target_json(self):
33
- try:
34
- parsed = json.loads(self.target_json)
35
- if 'functionCall' in parsed:
36
- return parsed['functionCall']
37
- except json.JSONDecodeError:
38
- pass
39
- return None
40
-
41
- # 使用示例
42
- extractor = JSONExtractor()
43
-
44
- # 模拟流式接收数据
45
- sample_lines = [
46
- '{\n',
47
- ' "candidates": [\n',
48
- ' {\n',
49
- ' "content": {\n',
50
- ' "parts": [\n',
51
- ' {\n',
52
- ' "functionCall": {\n',
53
- ' "name": "get_search_results",\n',
54
- ' "args": {\n',
55
- ' "prompt": "Claude Opus 3.5 release date"\n',
56
- ' }\n',
57
- ' }\n',
58
- ' }\n',
59
- ' ],\n',
60
- ' "role": "model"\n',
61
- ' },\n',
62
- ' "finishReason": "STOP",\n',
63
- ' "index": 0,\n',
64
- ' "safetyRatings": [\n',
65
- ' {\n',
66
- ' "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",\n',
67
- ' "probability": "NEGLIGIBLE"\n',
68
- ' },\n',
69
- ' {\n',
70
- ' "category": "HARM_CATEGORY_HATE_SPEECH",\n',
71
- ' "probability": "NEGLIGIBLE"\n',
72
- ' },\n',
73
- ' {\n',
74
- ' "category": "HARM_CATEGORY_HARASSMENT",\n',
75
- ' "probability": "NEGLIGIBLE"\n',
76
- ' },\n',
77
- ' {\n',
78
- ' "category": "HARM_CATEGORY_DANGEROUS_CONTENT",\n',
79
- ' "probability": "NEGLIGIBLE"\n',
80
- ' }\n',
81
- ' ]\n',
82
- ' }\n',
83
- ' ],\n',
84
- ' "usageMetadata": {\n',
85
- ' "promptTokenCount": 113,\n',
86
- ' "candidatesTokenCount": 55,\n',
87
- ' "totalTokenCount": 168\n',
88
- ' }\n',
89
- '}\n'
90
- ]
91
-
92
- for line in sample_lines:
93
- result = extractor.process_line(line)
94
- if result:
95
- print("提取的functionCall:")
96
- print(json.dumps(result, indent=2, ensure_ascii=False))
97
- break
@@ -1,21 +0,0 @@
1
- from collections import defaultdict
2
-
3
- # 定义一个默认值工厂函数,这里使用int来初始化为0
4
- default_dict = defaultdict(int)
5
-
6
- # 示例用法
7
- print(default_dict['a']) # 输出: 0,因为'a'不存在,自动初始化为0
8
- default_dict['a'] += 1
9
- print(default_dict['a']) # 输出: 1
10
-
11
- # 你也可以使用其他类型的工厂函数,例如list
12
- list_default_dict = defaultdict(list)
13
- print(list_default_dict['b']) # 输出: [],因为'b'不存在,自动初始化为空列表
14
- list_default_dict['b'].append(2)
15
- print(list_default_dict['b']) # 输出: [2]
16
-
17
- # 如果你有一个现有的字典,也可以将其转换为defaultdict
18
- existing_dict = {'c': 3, 'd': 4}
19
- default_dict = defaultdict(int, existing_dict)
20
- print(default_dict['c']) # 输出: 3
21
- print(default_dict['e']) # 输出: 0,因为'e'不存在,自动初始化为0
@@ -1,32 +0,0 @@
1
- import jieba
2
- import jieba.analyse
3
-
4
- # 加载文本
5
- # text = "话说葬送的芙莉莲动漫是半年番还是季番?完结没?"
6
- # text = "民进党当初为什么支持柯文哲选台北市长?"
7
- text = "今天的微博热搜有哪些?"
8
- # text = "How much does the 'zeabur' software service cost per month? Is it free to use? Any limitations?"
9
-
10
- # 使用TF-IDF算法提取关键词
11
- keywords_tfidf = jieba.analyse.extract_tags(text, topK=10, withWeight=False, allowPOS=())
12
-
13
- # 使用TextRank算法提取关键词
14
- keywords_textrank = jieba.analyse.textrank(text, topK=10, withWeight=False, allowPOS=('ns', 'n', 'vn', 'v'))
15
-
16
- print("TF-IDF算法提取的关键词:", keywords_tfidf)
17
- print("TextRank算法提取的关键词:", keywords_textrank)
18
-
19
-
20
- seg_list = jieba.cut(text, cut_all=True)
21
- print("Full Mode: " + " ".join(seg_list)) # 全模式
22
-
23
- seg_list = jieba.cut(text, cut_all=False)
24
- print("Default Mode: " + " ".join(seg_list)) # 精确模式
25
-
26
- seg_list = jieba.cut(text) # 默认是精确模式
27
- print(" ".join(seg_list))
28
-
29
- seg_list = jieba.cut_for_search(text) # 搜索引擎模式
30
- result = " ".join(seg_list)
31
-
32
- print([result] * 3)
@@ -1,65 +0,0 @@
1
- import json
2
-
3
- # json_data = '爱'
4
- # # json_data = '爱的主人,我会尽快为您规划一个走线到美国的安全路线。请您稍等片刻。\n\n首先,我会检查免签国家并为您提供相应的信息。接下来,我会 搜索有关旅行到美国的安全建议和路线规划。{}'
5
-
6
- def split_json_strings(input_string):
7
- # 初始化结果列表和当前 JSON 字符串
8
- json_strings = []
9
- current_json = ""
10
- brace_count = 0
11
-
12
- # 遍历输入字符串的每个字符
13
- for char in input_string:
14
- current_json += char
15
- if char == '{':
16
- brace_count += 1
17
- elif char == '}':
18
- brace_count -= 1
19
-
20
- # 如果花括号配对完成,我们找到了一个完整的 JSON 字符串
21
- if brace_count == 0:
22
- # 尝试解析当前 JSON 字符串
23
- try:
24
- json.loads(current_json)
25
- json_strings.append(current_json)
26
- current_json = ""
27
- except json.JSONDecodeError:
28
- # 如果解析失败,继续添加字符
29
- pass
30
- if json_strings == []:
31
- json_strings.append(input_string)
32
- return json_strings
33
-
34
- # 测试函数
35
- input_string = '{"url": "https://github.com/fastai/fasthtml"'
36
- result = split_json_strings(input_string)
37
-
38
- for i, json_str in enumerate(result, 1):
39
- print(f"JSON {i}:", json_str)
40
- print("Parsed:", json.loads(json_str))
41
- print()
42
-
43
- # def check_json(json_data):
44
- # while True:
45
- # try:
46
- # json.loads(json_data)
47
- # break
48
- # except json.decoder.JSONDecodeError as e:
49
- # print("JSON error:", e)
50
- # print("JSON body", repr(json_data))
51
- # if "Invalid control character" in str(e):
52
- # json_data = json_data.replace("\n", "\\n")
53
- # if "Unterminated string starting" in str(e):
54
- # json_data += '"}'
55
- # if "Expecting ',' delimiter" in str(e):
56
- # json_data += '}'
57
- # if "Expecting value: line 1 column 1" in str(e):
58
- # json_data = '{"prompt": ' + json.dumps(json_data) + '}'
59
- # return json_data
60
- # print(json.loads(check_json(json_data)))
61
-
62
- # a = '''
63
- # '''
64
-
65
- # print(json.loads(a))
@@ -1,32 +0,0 @@
1
- import logging
2
-
3
- class SpecificStringFilter(logging.Filter):
4
- def __init__(self, specific_string):
5
- super().__init__()
6
- self.specific_string = specific_string
7
-
8
- def filter(self, record):
9
- return self.specific_string not in record.getMessage()
10
-
11
- # 创建一个 logger
12
- logger = logging.getLogger('my_logger')
13
- logger.setLevel(logging.DEBUG)
14
-
15
- # 创建一个 console handler,并设置级别为 debug
16
- ch = logging.StreamHandler()
17
- # ch.setLevel(logging.DEBUG)
18
-
19
- # 创建一个 filter 实例
20
- specific_string = "httpx.RemoteProtocolError: Server disconnected without sending a response."
21
- my_filter = SpecificStringFilter(specific_string)
22
-
23
- # 将 filter 添加到 handler
24
- ch.addFilter(my_filter)
25
-
26
- # 将 handler 添加到 logger
27
- logger.addHandler(ch)
28
-
29
- # 测试日志消息
30
- logger.debug("This is a debug message.")
31
- logger.error("This message will be ignored: ignore me.httpx.RemoteProtocolError: Server disconnected without sending a response.")
32
- logger.info("Another info message.")