Skip to content

Commit

Permalink
feat: update retrieval and assistant
Browse files Browse the repository at this point in the history
  • Loading branch information
Dttbd authored and jameszyao committed Nov 1, 2024
1 parent d0d957d commit 563da8a
Show file tree
Hide file tree
Showing 76 changed files with 952 additions and 3,747 deletions.
32 changes: 0 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,38 +138,6 @@ taskingai.retrieval.delete_collection(collection_id=coll.collection_id)
print("Collection deleted.")
```

### Tools

The Tools module in TaskingAI is an essential suite designed to augment the capabilities of TaskingAI agents. Here is an example of how to create, run, and delete a tool action:

```python
import taskingai

# Define a schema for the tool action
OPENAPI_SCHEMA = {
# Schema definition goes here
}

# Create a tool action based on the defined schema
actions = taskingai.tool.bulk_create_actions(
openapi_schema=OPENAPI_SCHEMA,
authentication={"type": "none"},
)
action = actions[0]
print(f"Action created: {action.action_id}")

# Run the action for a test purpose
result = taskingai.tool.run_action(
action_id=action.action_id,
parameters={"number": 42}
)
print(f"Action result: {result}")

# Delete the action when done
taskingai.tool.delete_action(action_id=action.action_id)
print("Action deleted.")
```

## Contributing

We welcome contributions of all kinds. Please read our [Contributing Guidelines](./CONTRIBUTING.md) for more information on how to get started.
188 changes: 67 additions & 121 deletions examples/assistant/chat_with_assistant.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,110 +2,49 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "initial_id",
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import time\n",
"import taskingai\n",
"# Load TaskingAI API Key from environment variable"
],
"outputs": [],
"execution_count": null
]
},
{
"cell_type": "markdown",
"id": "4ca20b4a868dedd8",
"metadata": {
"collapsed": false
},
"source": [
"# TaskingAI: Chat with Assistant Example\n",
"\n",
"In this example, we will first create an assistant who knows the meaning of various numbers and will explain it in certain language.\n",
"Then we will start a chat with the assistant."
],
"metadata": {
"collapsed": false
},
"id": "4ca20b4a868dedd8"
]
},
{
"cell_type": "markdown",
"source": [
"## Create Assistant"
],
"id": "5e19ac923d84e898",
"metadata": {
"collapsed": false
},
"id": "5e19ac923d84e898"
"source": [
"## Create Assistant"
]
},
{
"cell_type": "code",
"source": [
"from taskingai.tool import Action, ActionAuthentication, ActionAuthenticationType\n",
"from typing import List\n",
"\n",
"# create an assistant action\n",
"NUMBERS_API_SCHEMA = {\n",
" \"openapi\": \"3.0.0\",\n",
" \"info\": {\n",
" \"title\": \"Numbers API\",\n",
" \"version\": \"1.0.0\",\n",
" \"description\": \"API for fetching interesting number facts\"\n",
" },\n",
" \"servers\": [\n",
" {\n",
" \"url\": \"http://numbersapi.com\"\n",
" }\n",
" ],\n",
" \"paths\": {\n",
" \"/{number}\": {\n",
" \"get\": {\n",
" \"description\": \"Get a fact about a number\",\n",
" \"operationId\": \"getNumberFact\",\n",
" \"parameters\": [\n",
" {\n",
" \"name\": \"number\",\n",
" \"in\": \"path\",\n",
" \"required\": True,\n",
" \"description\": \"The number to get the fact for\",\n",
" \"schema\": {\n",
" \"type\": \"integer\"\n",
" }\n",
" }\n",
" ],\n",
" \"responses\": {\n",
" \"200\": {\n",
" \"description\": \"A fact about the number\",\n",
" \"content\": {\n",
" \"text/plain\": {\n",
" \"schema\": {\n",
" \"type\": \"string\"\n",
" }\n",
" }\n",
" }\n",
" }\n",
" }\n",
" }\n",
" }\n",
" }\n",
"}\n",
"actions: List[Action] = taskingai.tool.bulk_create_actions(\n",
" openapi_schema=NUMBERS_API_SCHEMA,\n",
" authentication=ActionAuthentication(\n",
" type=ActionAuthenticationType.NONE,\n",
" )\n",
")\n",
"action = actions[0]\n",
"print(f\"created action: {action}\\n\")"
],
"execution_count": null,
"id": "3b3df0f232021283",
"metadata": {
"collapsed": false
},
"id": "3b2fda39ba58c5e9",
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": [
"from taskingai.assistant import Assistant, Chat, ToolRef, ToolType\n",
"from taskingai.assistant.memory import AssistantMessageWindowMemory\n",
Expand All @@ -118,7 +57,6 @@
" name=\"My Assistant\",\n",
" description=\"A assistant who knows the meaning of various numbers.\",\n",
" memory=AssistantMessageWindowMemory(\n",
" max_messages=20,\n",
" max_tokens=1000\n",
" ),\n",
" system_prompt_template=[\n",
Expand All @@ -127,49 +65,49 @@
" ],\n",
" tools=[\n",
" ToolRef(\n",
" type=ToolType.ACTION,\n",
" id=action.action_id,\n",
" type=ToolType.PLUGIN,\n",
" id=\"open_weather/get_hourly_forecast\",\n",
" )\n",
" ],\n",
" retrievals=[],\n",
" metadata={\"k\": \"v\"},\n",
")\n",
"print(f\"created assistant: {assistant}\\n\")"
],
"metadata": {
"collapsed": false
},
"id": "3b3df0f232021283",
"outputs": [],
"execution_count": null
]
},
{
"cell_type": "markdown",
"source": [
"## Start a Chat "
],
"id": "8e7c1b9461f0a344",
"metadata": {
"collapsed": false
},
"id": "8e7c1b9461f0a344"
"source": [
"## Start a Chat "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1e2f0b2af8b1d8d",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"chat: Chat = taskingai.assistant.create_chat(\n",
" assistant_id=assistant.assistant_id,\n",
")\n",
"print(f\"created chat: {chat.chat_id}\\n\")"
],
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b26e30b79b71697a",
"metadata": {
"collapsed": false
},
"id": "f1e2f0b2af8b1d8d",
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": [
"from taskingai.assistant import Message, MessageChunk\n",
"user_input = input(\"User Input: \")\n",
Expand All @@ -181,7 +119,7 @@
" text=user_input,\n",
" )\n",
" print(f\"User: {user_input}\")\n",
" \n",
"\n",
" # generate assistant response\n",
" assistant_message: Message = taskingai.assistant.generate_message(\n",
" assistant_id=assistant.assistant_id,\n",
Expand All @@ -194,16 +132,16 @@
" time.sleep(2)\n",
" # quit by input 'q\n",
" user_input = input(\"User: \")"
],
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7d73e0b138e3eba",
"metadata": {
"collapsed": false
},
"id": "b26e30b79b71697a",
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": [
"user_input = input(\"User Input: \")\n",
"while user_input.strip() and user_input != \"q\":\n",
Expand All @@ -214,7 +152,7 @@
" text=user_input,\n",
" )\n",
" print(f\"User: {user_input} ({user_message.message_id})\")\n",
" \n",
"\n",
" # generate assistant response\n",
" assistant_message_response = taskingai.assistant.generate_message(\n",
" assistant_id=assistant.assistant_id,\n",
Expand All @@ -224,27 +162,37 @@
" },\n",
" stream=True,\n",
" )\n",
" \n",
" print(f\"Assistant:\", end=\" \", flush=True)\n",
"\n",
" print(\"Assistant:\", end=\" \", flush=True)\n",
" for item in assistant_message_response:\n",
" if isinstance(item, MessageChunk):\n",
" print(item.delta, end=\"\", flush=True)\n",
" elif isinstance(item, Message):\n",
" print(f\" ({item.message_id})\")\n",
" \n",
"\n",
" time.sleep(2)\n",
" # quit by input 'q\n",
" user_input = input(\"User: \")"
],
"metadata": {
"collapsed": false
},
"id": "c7d73e0b138e3eba",
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a67261c",
"metadata": {},
"outputs": [],
"execution_count": null
"source": [
"# clean chat context\n",
"taskingai.assistant.clean_chat_context(\n",
" assistant_id=assistant.assistant_id,\n",
" chat_id=chat.chat_id,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"# list messages\n",
"messages = taskingai.assistant.list_messages(\n",
Expand All @@ -258,12 +206,12 @@
"metadata": {
"collapsed": false
},
"id": "e94e3adb0d15373b",
"outputs": [],
"execution_count": null
"id": "e94e3adb0d15373b"
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"# delete assistant\n",
"taskingai.assistant.delete_assistant(\n",
Expand All @@ -273,9 +221,7 @@
"metadata": {
"collapsed": false
},
"id": "ed39836bbfdc7a4e",
"outputs": [],
"execution_count": null
"id": "ed39836bbfdc7a4e"
}
],
"metadata": {
Expand All @@ -287,14 +233,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 563da8a

Please sign in to comment.