Skip to content

Commit

Permalink
feat: add collection type and clean chat context
Browse files Browse the repository at this point in the history
  • Loading branch information
Dttbd committed Oct 30, 2024
1 parent d0d957d commit 329dd23
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 66 deletions.
142 changes: 78 additions & 64 deletions examples/assistant/chat_with_assistant.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +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",
"execution_count": null,
"id": "3b2fda39ba58c5e9",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from taskingai.tool import Action, ActionAuthentication, ActionAuthenticationType\n",
"from typing import List\n",
Expand Down Expand Up @@ -96,16 +102,16 @@
")\n",
"action = actions[0]\n",
"print(f\"created action: {action}\\n\")"
],
]
},
{
"cell_type": "code",
"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 Down Expand Up @@ -135,41 +141,41 @@
" 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 +187,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 +200,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 +220,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 +230,27 @@
" },\n",
" stream=True,\n",
" )\n",
" \n",
"\n",
" print(f\"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: \")"
],
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e94e3adb0d15373b",
"metadata": {
"collapsed": false
},
"id": "c7d73e0b138e3eba",
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": [
"# list messages\n",
"messages = taskingai.assistant.list_messages(\n",
Expand All @@ -254,28 +260,36 @@
")\n",
"for message in messages:\n",
" print(f\"{message.role}: {message.content.text}\")"
],
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ed39836bbfdc7a4e",
"metadata": {
"collapsed": false
},
"id": "e94e3adb0d15373b",
"outputs": [],
"execution_count": null
},
{
"cell_type": "code",
"source": [
"# delete assistant\n",
"taskingai.assistant.delete_assistant(\n",
" assistant_id=assistant.assistant_id,\n",
")"
],
"metadata": {
"collapsed": false
},
"id": "ed39836bbfdc7a4e",
]
},
{
"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=\"YOUR_ASSISTANT_ID\",\n",
" chat_id=\"YOUR_CHAT_ID\",\n",
")"
]
}
],
"metadata": {
Expand All @@ -294,7 +308,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
24 changes: 24 additions & 0 deletions taskingai/assistant/assistant.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"a_create_assistant",
"a_update_assistant",
"a_delete_assistant",
"clean_chat_context",
"a_clean_chat_context",
]

AssistantTool = ToolRef
Expand Down Expand Up @@ -344,3 +346,25 @@ async def a_delete_assistant(assistant_id: str) -> None:
"""

await async_api_delete_assistant(assistant_id=assistant_id)


def clean_chat_context(assistant_id: str, chat_id: str) -> None:
"""
Clean chat context.
:param assistant_id: The ID of the assistant.
:param chat_id: The ID of the chat.
"""

api_clean_chat_context(assistant_id=assistant_id, chat_id=chat_id)


async def a_clean_chat_context(assistant_id: str, chat_id: str) -> None:
"""
Clean chat context in async mode.
:param assistant_id: The ID of the assistant.
:param chat_id: The ID of the chat.
"""

await async_api_clean_chat_context(assistant_id=assistant_id, chat_id=chat_id)
1 change: 1 addition & 0 deletions taskingai/client/apis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .api_bulk_create_actions import *
from .api_chat_completion import *
from .api_clean_chat_context import *
from .api_create_assistant import *
from .api_create_chat import *
from .api_create_chunk import *
Expand Down
Loading

0 comments on commit 329dd23

Please sign in to comment.