Skip to content

Commit 81c824a

Browse files
committed
feat: implement file upload with rest client
1 parent 8c20f8a commit 81c824a

19 files changed

+696
-491
lines changed

examples/crud/assistant_crud.ipynb

+5-7
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
"def create_assistant() -> Assistant:\n",
5454
" assistant: Assistant = taskingai.assistant.create_assistant(\n",
5555
" model_id=model_id,\n",
56-
" name=\"My Assistant\",\n",
57-
" description=\"This is my assistant\",\n",
58-
" system_prompt_template=[\"You are a professional assistant speaking {{language}}.\"],\n",
56+
" name=\"Customer Service Assistant\",\n",
57+
" description=\"A professional assistant for customer service.\",\n",
58+
" system_prompt_template=[\"You are a professional customer service assistant speaking {{language}}.\"],\n",
5959
" memory=AssistantNaiveMemory(),\n",
6060
" tools=[],\n",
6161
" retrievals=[],\n",
@@ -96,8 +96,7 @@
9696
"# update assistant\n",
9797
"assistant: Assistant = taskingai.assistant.update_assistant(\n",
9898
" assistant_id=assistant_id,\n",
99-
" name=\"My New Assistant\",\n",
100-
" description=\"This is my new assistant\",\n",
99+
" name=\"New Assistant\",\n",
101100
" retrieval_configs=RetrievalConfig(top_k=4, max_tokens=8192, method=RetrievalMethod.USER_MESSAGE),\n",
102101
")\n",
103102
"\n",
@@ -155,7 +154,6 @@
155154
"# create a chat\n",
156155
"chat: Chat = taskingai.assistant.create_chat(\n",
157156
" assistant_id=assistant.assistant_id,\n",
158-
" name=\"my chat\",\n",
159157
")\n",
160158
"print(f\"created chat: {chat.chat_id} for assistant: {assistant.assistant_id}\\n\")"
161159
]
@@ -189,7 +187,7 @@
189187
"chat: Chat = taskingai.assistant.update_chat(\n",
190188
" assistant_id=assistant.assistant_id,\n",
191189
" chat_id=chat_id,\n",
192-
" name=\"my chat new name\",\n",
190+
" name=\"New Chat\",\n",
193191
" metadata={\"foo\": \"bar\"},\n",
194192
")\n",
195193
"print(f\"updated chat: {chat}\\n\")"

examples/crud/retrieval_crud.ipynb

+34-13
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@
240240
"record: Record = taskingai.retrieval.create_record(\n",
241241
" collection_id=collection.collection_id,\n",
242242
" type=\"web\",\n",
243-
" title=\"Machine learning\",\n",
243+
" title=\"Tasking AI\",\n",
244244
" url=\"https://www.tasking.ai\", # must https\n",
245245
" text_splitter={\"type\": \"token\", \"chunk_size\": 200, \"chunk_overlap\": 20},\n",
246246
")\n",
@@ -259,6 +259,7 @@
259259
" collection_id=collection.collection_id,\n",
260260
" record_id=record.record_id,\n",
261261
" type=\"web\",\n",
262+
" title=\"Tasking Documentations\",\n",
262263
" url=\"https://docs.tasking.ai\",\n",
263264
" text_splitter={\"type\": \"token\", \"chunk_size\": 200, \"chunk_overlap\": 20},\n",
264265
")\n",
@@ -283,21 +284,42 @@
283284
"# upload a file first\n",
284285
"from taskingai.file import upload_file\n",
285286
"\n",
286-
"file = upload_file(file=\"your file path\", purpose=\"record_file\")\n",
287-
"# or\n",
288-
"# file = upload_file(file=open(\"your file path\", \"rb\"), purpose=\"record_file\")\n",
289-
"print(f\"uploaded file id: {file.file_id}\")\n",
290-
"\n",
291-
"# create a new web record\n",
287+
"file = upload_file(file=open(\"YOUR_FILE_PATH\", \"rb\"), purpose=\"record_file\")\n",
288+
"print(f\"uploaded file id: {file.file_id}\")"
289+
]
290+
},
291+
{
292+
"cell_type": "code",
293+
"execution_count": null,
294+
"outputs": [],
295+
"source": [
296+
"# create a new file record\n",
292297
"record: Record = taskingai.retrieval.create_record(\n",
293298
" collection_id=collection.collection_id,\n",
294299
" type=\"file\",\n",
295-
" title=\"Machine learning\",\n",
300+
" title=\"Machine Learning\",\n",
296301
" file_id=file.file_id,\n",
297302
" text_splitter={\"type\": \"token\", \"chunk_size\": 200, \"chunk_overlap\": 20},\n",
298303
")\n",
299304
"print(f\"created record: {record.record_id} for collection: {collection.collection_id}\\n\")"
300-
]
305+
],
306+
"metadata": {
307+
"collapsed": false
308+
},
309+
"id": "832ae91419da5493"
310+
},
311+
{
312+
"cell_type": "code",
313+
"execution_count": null,
314+
"outputs": [],
315+
"source": [
316+
"new_file = upload_file(file=open(\"NEW_FILE_PATH\", \"rb\"), purpose=\"record_file\")\n",
317+
"print(f\"new uploaded file id: {new_file.file_id}\")"
318+
],
319+
"metadata": {
320+
"collapsed": false
321+
},
322+
"id": "8176058e6c15a1e0"
301323
},
302324
{
303325
"cell_type": "code",
@@ -306,14 +328,12 @@
306328
"metadata": {},
307329
"outputs": [],
308330
"source": [
309-
"new_file = upload_file(file=\"new_file_path\", purpose=\"record_file\")\n",
310-
"print(f\"new uploaded file id: {new_file.file_id}\")\n",
311-
"\n",
312331
"# update record - file\n",
313332
"record = taskingai.retrieval.update_record(\n",
314333
" collection_id=collection.collection_id,\n",
315334
" record_id=record.record_id,\n",
316335
" type=\"file\",\n",
336+
" title=\"Deep Learning\",\n",
317337
" file_id=new_file.file_id,\n",
318338
" text_splitter={\"type\": \"token\", \"chunk_size\": 200, \"chunk_overlap\": 20},\n",
319339
")\n",
@@ -325,7 +345,7 @@
325345
"id": "15465ad8",
326346
"metadata": {},
327347
"source": [
328-
"### Record Other Cases"
348+
"### Other Operations"
329349
]
330350
},
331351
{
@@ -503,6 +523,7 @@
503523
"# create a new text record and a new chunk\n",
504524
"taskingai.retrieval.create_record(\n",
505525
" collection_id=collection.collection_id,\n",
526+
" type=\"text\",\n",
506527
" content=\"Machine learning is a subfield of artificial intelligence (AI) that involves the development of algorithms that allow computers to learn from and make decisions or predictions based on data. The term \\\"machine learning\\\" was coined by Arthur Samuel in 1959. In other words, machine learning enables a system to automatically learn and improve from experience without being explicitly programmed. This is achieved by feeding the system massive amounts of data, which it uses to learn patterns and make inferences. There are three main types of machine learning: 1. Supervised Learning: This is where the model is given labeled training data and the goal of learning is to generalize from the training data to unseen situations in a principled way. 2. Unsupervised Learning: This involves training on a dataset without explicit labels. The goal might be to discover inherent groupings or patterns within the data. 3. Reinforcement Learning: In this type, an agent learns to perform actions based on reward/penalty feedback to achieve a goal. It's commonly used in robotics, gaming, and navigation. Deep learning, a subset of machine learning, uses neural networks with many layers (\\\"deep\\\" structures) and has been responsible for many recent breakthroughs in AI, including speech recognition, image recognition, and natural language processing. It's important to note that machine learning is a rapidly developing field, with new techniques and applications emerging regularly.\",\n",
507528
" text_splitter=TokenTextSplitter(chunk_size=200, chunk_overlap=20)\n",
508529
")\n",

examples/retrieval/semantic_search.ipynb

+3-10
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
"# create record 1 (machine learning)\n",
9393
"taskingai.retrieval.create_record(\n",
9494
" collection_id=collection.collection_id,\n",
95+
" type=\"text\",\n",
9596
" content=\"Machine learning is a subfield of artificial intelligence (AI) that involves the development of algorithms that allow computers to learn from and make decisions or predictions based on data. The term \\\"machine learning\\\" was coined by Arthur Samuel in 1959. In other words, machine learning enables a system to automatically learn and improve from experience without being explicitly programmed. This is achieved by feeding the system massive amounts of data, which it uses to learn patterns and make inferences. There are three main types of machine learning: 1. Supervised Learning: This is where the model is given labeled training data and the goal of learning is to generalize from the training data to unseen situations in a principled way. 2. Unsupervised Learning: This involves training on a dataset without explicit labels. The goal might be to discover inherent groupings or patterns within the data. 3. Reinforcement Learning: In this type, an agent learns to perform actions based on reward/penalty feedback to achieve a goal. It's commonly used in robotics, gaming, and navigation. Deep learning, a subset of machine learning, uses neural networks with many layers (\\\"deep\\\" structures) and has been responsible for many recent breakthroughs in AI, including speech recognition, image recognition, and natural language processing. It's important to note that machine learning is a rapidly developing field, with new techniques and applications emerging regularly.\",\n",
9697
" text_splitter=TokenTextSplitter(\n",
9798
" chunk_size=100, # maximum tokens of each chunk\n",
@@ -112,6 +113,7 @@
112113
"# create record 2 (Michael Jordan)\n",
113114
"taskingai.retrieval.create_record(\n",
114115
" collection_id=collection.collection_id,\n",
116+
" type=\"text\",\n",
115117
" content=\"Michael Jordan, often referred to by his initials MJ, is considered one of the greatest players in the history of the National Basketball Association (NBA). He was known for his scoring ability, defensive prowess, competitiveness, and clutch performances. Born on February 17, 1963, Jordan played 15 seasons in the NBA, primarily with the Chicago Bulls, but also with the Washington Wizards. His professional career spanned two decades from 1984 to 2003, during which he won numerous awards and set multiple records. Here are some key highlights of his career: - Scoring: Jordan won the NBA scoring title a record 10 times. He also has the highest career scoring average in NBA history, both in the regular season (30.12 points per game) and in the playoffs (33.45 points per game). - Championships: He led the Chicago Bulls to six NBA championships and was named Finals MVP in all six of those Finals (1991-1993, 1996-1998). - MVP Awards: Jordan was named the NBA's Most Valuable Player (MVP) five times (1988, 1991, 1992, 1996, 1998). - Defensive Ability: He was named to the NBA All-Defensive First Team nine times and won the NBA Defensive Player of the Year award in 1988. - Olympics: Jordan also won two Olympic gold medals with the U.S. basketball team, in 1984 and 1992. - Retirements and Comebacks: Jordan retired twice during his career. His first retirement came in 1993, after which he briefly played minor league baseball. He returned to the NBA in 1995. He retired a second time in 1999, only to return again in 2001, this time with the Washington Wizards. He played two seasons for the Wizards before retiring for good in 2003. After his playing career, Jordan became a team owner and executive. As of my knowledge cutoff in September 2021, he is the majority owner of the Charlotte Hornets. Off the court, Jordan is known for his lucrative endorsement deals, particularly with Nike. The Air Jordan line of sneakers is one of the most popular and enduring in the world. His influence also extends to the realms of film and fashion, and he is recognized globally as a cultural icon. In 2000, he was inducted into the Basketball Hall of Fame.\",\n",
116118
" text_splitter=TokenTextSplitter(\n",
117119
" chunk_size=100,\n",
@@ -132,6 +134,7 @@
132134
"# create record 3 (Granite)\n",
133135
"taskingai.retrieval.create_record(\n",
134136
" collection_id=collection.collection_id,\n",
137+
" type=\"text\",\n",
135138
" content=\"Granite is a type of coarse-grained igneous rock composed primarily of quartz and feldspar, among other minerals. The term \\\"granitic\\\" means granite-like and is applied to granite and a group of intrusive igneous rocks. Description of Granite * Type: Igneous rock * Grain size: Coarse-grained * Composition: Mainly quartz, feldspar, and micas with minor amounts of amphibole minerals * Color: Typically appears in shades of white, pink, or gray, depending on their mineralogy * Crystalline Structure: Yes, due to slow cooling of magma beneath Earth's surface * Density: Approximately 2.63 to 2.75 g/cm³ * Hardness: 6-7 on the Mohs hardness scale Formation Process Granite is formed from the slow cooling of magma that is rich in silica and aluminum, deep beneath the earth's surface. Over time, the magma cools slowly, allowing large crystals to form and resulting in the coarse-grained texture that is characteristic of granite. Uses Granite is known for its durability and aesthetic appeal, making it a popular choice for construction and architectural applications. It's often used for countertops, flooring, monuments, and building materials. In addition, due to its hardness and toughness, it is used for cobblestones and in other paving applications. Geographical Distribution Granite is found worldwide, with significant deposits in regions such as the United States (especially in New Hampshire, which is also known as \\\"The Granite State\\\"), Canada, Brazil, Norway, India, and China. Varieties There are many varieties of granite, based on differences in color and mineral composition. Some examples include Bianco Romano, Black Galaxy, Blue Pearl, Santa Cecilia, and Ubatuba. Each variety has unique patterns, colors, and mineral compositions.\",\n",
136139
" text_splitter=TokenTextSplitter(\n",
137140
" chunk_size=100,\n",
@@ -224,16 +227,6 @@
224227
"collapsed": false
225228
},
226229
"id": "fc9c1fa12d893dd1"
227-
},
228-
{
229-
"cell_type": "code",
230-
"execution_count": null,
231-
"outputs": [],
232-
"source": [],
233-
"metadata": {
234-
"collapsed": false
235-
},
236-
"id": "e0eb39fcac309768"
237230
}
238231
],
239232
"metadata": {

requirements.txt

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,4 @@ python_dateutil>=2.5.3
44
setuptools>=21.0.0
55
httpx>=0.23.0
66
pydantic>=2.5.0
7-
wheel==0.43.0
8-
aiofiles==23.2.1
9-
aiohttp==3.9.5
10-
requests==2.31.0
7+
wheel>=0.43.0

taskingai/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from . import tool
44
from . import retrieval
55
from . import inference
6+
from . import file
67
from ._version import __version__
78

89
__all__ = [
@@ -11,4 +12,4 @@
1112
"retrieval",
1213
"inference",
1314
"__version__",
14-
]
15+
]

taskingai/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__title__ = "taskingai"
2-
__version__ = "0.2.1"
2+
__version__ = "0.2.2"

taskingai/assistant/assistant.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def _get_assistant_dict_params(
5656

5757

5858
def list_assistants(
59+
*,
5960
order: str = "desc",
6061
limit: int = 20,
6162
after: Optional[str] = None,
@@ -87,13 +88,14 @@ def list_assistants(
8788

8889

8990
async def a_list_assistants(
91+
*,
9092
order: str = "desc",
9193
limit: int = 20,
9294
after: Optional[str] = None,
9395
before: Optional[str] = None,
9496
) -> List[Assistant]:
9597
"""
96-
List assistants.
98+
List assistants in async mode.
9799
98100
:param order: The order of the assistants. It can be "asc" or "desc".
99101
:param limit: The maximum number of assistants to return.
@@ -139,6 +141,7 @@ async def a_get_assistant(assistant_id: str) -> Assistant:
139141

140142

141143
def create_assistant(
144+
*,
142145
model_id: str,
143146
memory: Union[AssistantMemory, Dict[str, Any]],
144147
name: Optional[str] = None,
@@ -159,6 +162,7 @@ def create_assistant(
159162
:param system_prompt_template: A list of system prompt chunks where prompt variables are wrapped by curly brackets, e.g. {{variable}}.
160163
:param tools: The assistant tools.
161164
:param retrievals: The assistant retrievals.
165+
:param retrieval_configs: The assistant retrieval configurations.
162166
:param metadata: The assistant metadata. It can store up to 16 key-value pairs where each key's length is less than 64 and value's length is less than 512.
163167
:return: The created assistant object.
164168
"""
@@ -182,6 +186,7 @@ def create_assistant(
182186

183187

184188
async def a_create_assistant(
189+
*,
185190
model_id: str,
186191
memory: Union[AssistantMemory, Dict[str, Any]],
187192
name: Optional[str] = None,
@@ -202,6 +207,7 @@ async def a_create_assistant(
202207
:param system_prompt_template: A list of system prompt chunks where prompt variables are wrapped by curly brackets, e.g. {{variable}}.
203208
:param tools: The assistant tools.
204209
:param retrievals: The assistant retrievals.
210+
:param retrieval_configs: The assistant retrieval configurations.
205211
:param metadata: The assistant metadata. It can store up to 16 key-value pairs where each key's length is less than 64 and value's length is less than 512.
206212
:return: The created assistant object.
207213
"""
@@ -226,6 +232,7 @@ async def a_create_assistant(
226232

227233
def update_assistant(
228234
assistant_id: str,
235+
*,
229236
model_id: Optional[str] = None,
230237
name: Optional[str] = None,
231238
description: Optional[str] = None,
@@ -247,6 +254,7 @@ def update_assistant(
247254
:param memory: The assistant memory.
248255
:param tools: The assistant tools.
249256
:param retrievals: The assistant retrievals.
257+
:param retrieval_configs: The assistant retrieval configurations.
250258
:param metadata: The assistant metadata. It can store up to 16 key-value pairs where each key's length is less than 64 and value's length is less than 512.
251259
:return: The updated assistant object.
252260
"""
@@ -272,6 +280,7 @@ def update_assistant(
272280

273281
async def a_update_assistant(
274282
assistant_id: str,
283+
*,
275284
model_id: Optional[str] = None,
276285
name: Optional[str] = None,
277286
description: Optional[str] = None,
@@ -293,6 +302,7 @@ async def a_update_assistant(
293302
:param memory: The assistant memory.
294303
:param tools: The assistant tools.
295304
:param retrievals: The assistant retrievals.
305+
:param retrieval_configs: The assistant retrieval configurations.
296306
:param metadata: The assistant metadata. It can store up to 16 key-value pairs where each key's length is less than 64 and value's length is less than 512.
297307
:return: The updated assistant object.
298308
"""

taskingai/assistant/chat.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
def list_chats(
2222
assistant_id: str,
23+
*,
2324
order: str = "desc",
2425
limit: int = 20,
2526
after: Optional[str] = None,
@@ -53,6 +54,7 @@ def list_chats(
5354

5455
async def a_list_chats(
5556
assistant_id: str,
57+
*,
5658
order: str = "desc",
5759
limit: int = 20,
5860
after: Optional[str] = None,
@@ -116,7 +118,8 @@ async def a_get_chat(assistant_id: str, chat_id: str) -> Chat:
116118

117119
def create_chat(
118120
assistant_id: str,
119-
name: str,
121+
*,
122+
name: Optional[str] = None,
120123
metadata: Optional[Dict[str, str]] = None,
121124
) -> Chat:
122125
"""
@@ -129,7 +132,7 @@ def create_chat(
129132
"""
130133

131134
body = ChatCreateRequest(
132-
name=name,
135+
name=name or "",
133136
metadata=metadata or {},
134137
)
135138
response: ChatCreateResponse = api_create_chat(assistant_id=assistant_id, payload=body)
@@ -138,7 +141,8 @@ def create_chat(
138141

139142
async def a_create_chat(
140143
assistant_id: str,
141-
name: str,
144+
*,
145+
name: Optional[str] = None,
142146
metadata: Optional[Dict[str, str]] = None,
143147
) -> Chat:
144148
"""
@@ -151,7 +155,7 @@ async def a_create_chat(
151155
"""
152156

153157
body = ChatCreateRequest(
154-
name=name,
158+
name=name or "",
155159
metadata=metadata or {},
156160
)
157161
response: ChatCreateResponse = await async_api_create_chat(assistant_id=assistant_id, payload=body)
@@ -161,6 +165,7 @@ async def a_create_chat(
161165
def update_chat(
162166
assistant_id: str,
163167
chat_id: str,
168+
*,
164169
name: Optional[str] = None,
165170
metadata: Optional[Dict[str, str]] = None,
166171
) -> Chat:
@@ -185,6 +190,7 @@ def update_chat(
185190
async def a_update_chat(
186191
assistant_id: str,
187192
chat_id: str,
193+
*,
188194
name: Optional[str] = None,
189195
metadata: Optional[Dict[str, str]] = None,
190196
) -> Chat:

0 commit comments

Comments
 (0)