fix delete chat bug
This commit is contained in:
@@ -209,47 +209,47 @@ async def get_chat_records():
|
|||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
"""Run all examples."""
|
"""Run all examples."""
|
||||||
# print("=== Simple Chat ===")
|
print("=== Simple Chat ===")
|
||||||
# try:
|
try:
|
||||||
# await simple_chat()
|
await simple_chat()
|
||||||
# except Exception as e:
|
except Exception as e:
|
||||||
# print(f"Error: {e}")
|
print(f"Error: {e}")
|
||||||
|
|
||||||
# print("\n=== Streaming Chat ===")
|
print("\n=== Streaming Chat ===")
|
||||||
# try:
|
try:
|
||||||
# await streaming_chat()
|
await streaming_chat()
|
||||||
# except Exception as e:
|
except Exception as e:
|
||||||
# print(f"Error: {e}")
|
print(f"Error: {e}")
|
||||||
|
|
||||||
# print("\n=== Chat with Context ===")
|
print("\n=== Chat with Context ===")
|
||||||
# try:
|
try:
|
||||||
# await chat_with_context()
|
await chat_with_context()
|
||||||
# except Exception as e:
|
except Exception as e:
|
||||||
# print(f"Error: {e}")
|
print(f"Error: {e}")
|
||||||
|
|
||||||
# print("\n=== Get Histories ===")
|
print("\n=== Get Histories ===")
|
||||||
# try:
|
try:
|
||||||
# await get_histories()
|
await get_histories()
|
||||||
# except Exception as e:
|
except Exception as e:
|
||||||
# print(f"Error: {e}")
|
print(f"Error: {e}")
|
||||||
|
|
||||||
# print("\n=== Get App Analytics ===")
|
print("\n=== Get App Analytics ===")
|
||||||
# try:
|
try:
|
||||||
# await get_app_analytics()
|
await get_app_analytics()
|
||||||
# except Exception as e:
|
except Exception as e:
|
||||||
# print(f"Error: {e}")
|
print(f"Error: {e}")
|
||||||
|
|
||||||
# print("\n=== Multiple Requests (Concurrent) ===")
|
print("\n=== Multiple Requests (Concurrent) ===")
|
||||||
# try:
|
try:
|
||||||
# await multiple_requests()
|
await multiple_requests()
|
||||||
# except Exception as e:
|
except Exception as e:
|
||||||
# print(f"Error: {e}")
|
print(f"Error: {e}")
|
||||||
|
|
||||||
# print("\n=== Chat with Variables ===")
|
print("\n=== Chat with Variables ===")
|
||||||
# try:
|
try:
|
||||||
# await chat_with_variables()
|
await chat_with_variables()
|
||||||
# except Exception as e:
|
except Exception as e:
|
||||||
# print(f"Error: {e}")
|
print(f"Error: {e}")
|
||||||
|
|
||||||
print("\n=== Get Chat Records ===")
|
print("\n=== Get Chat Records ===")
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -99,27 +99,86 @@ def get_histories():
|
|||||||
print(f"Error: {e}")
|
print(f"Error: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
def delete_chat_item():
|
||||||
|
"""Delete a chat item (record) example."""
|
||||||
|
with ChatClient(api_key=API_KEY, base_url=BASE_URL) as client:
|
||||||
|
app_id = os.getenv("APP_ID")
|
||||||
|
chat_id = os.getenv("CHAT_ID")
|
||||||
|
|
||||||
|
# First, get the chat records to find a contentId to delete
|
||||||
|
try:
|
||||||
|
records = client.get_chat_records(
|
||||||
|
appId=app_id,
|
||||||
|
chatId=chat_id,
|
||||||
|
offset=0,
|
||||||
|
pageSize=10
|
||||||
|
)
|
||||||
|
records.raise_for_status()
|
||||||
|
data = records.json()
|
||||||
|
|
||||||
|
if data.get('data', {}).get('list'):
|
||||||
|
# Get the first record's dataId as contentId
|
||||||
|
content_id = data['data']['list'][0].get('dataId')
|
||||||
|
|
||||||
|
if content_id:
|
||||||
|
print(f"Deleting chat item with contentId: {content_id}")
|
||||||
|
print(f"Using appId: {app_id}, chatId: {chat_id}")
|
||||||
|
try:
|
||||||
|
response = client.delete_chat_record(
|
||||||
|
appId=app_id,
|
||||||
|
chatId=chat_id,
|
||||||
|
contentId=content_id
|
||||||
|
)
|
||||||
|
print(f"Response status: {response.status_code}")
|
||||||
|
print(f"Response text: {response.text}")
|
||||||
|
response.raise_for_status()
|
||||||
|
print("Chat item deleted successfully!")
|
||||||
|
except Exception as delete_error:
|
||||||
|
print(f"Delete error details:")
|
||||||
|
print(f" Error type: {type(delete_error).__name__}")
|
||||||
|
print(f" Error message: {delete_error}")
|
||||||
|
# Try to get response details if available
|
||||||
|
if hasattr(delete_error, 'response'):
|
||||||
|
print(f" Response status: {delete_error.response.status_code}")
|
||||||
|
print(f" Response text: {delete_error.response.text}")
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
print("No contentId found in the record")
|
||||||
|
else:
|
||||||
|
print("No chat records found to delete")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("=== Simple Chat ===")
|
# print("=== Simple Chat ===")
|
||||||
try:
|
# try:
|
||||||
simple_chat()
|
# simple_chat()
|
||||||
except Exception as e:
|
# except Exception as e:
|
||||||
print(f"Error: {e}")
|
# print(f"Error: {e}")
|
||||||
|
|
||||||
print("\n=== Streaming Chat ===")
|
# print("\n=== Streaming Chat ===")
|
||||||
try:
|
# try:
|
||||||
streaming_chat()
|
# streaming_chat()
|
||||||
except Exception as e:
|
# except Exception as e:
|
||||||
print(f"Error: {e}")
|
# print(f"Error: {e}")
|
||||||
|
|
||||||
print("\n=== Chat with Context ===")
|
# print("\n=== Chat with Context ===")
|
||||||
try:
|
# try:
|
||||||
chat_with_context()
|
# chat_with_context()
|
||||||
except Exception as e:
|
# except Exception as e:
|
||||||
print(f"Error: {e}")
|
# print(f"Error: {e}")
|
||||||
|
|
||||||
print("\n=== Get Histories ===")
|
# print("\n=== Get Histories ===")
|
||||||
|
# try:
|
||||||
|
# get_histories()
|
||||||
|
# except Exception as e:
|
||||||
|
# print(f"Error: {e}")
|
||||||
|
|
||||||
|
print("\n=== Delete Chat Item ===")
|
||||||
try:
|
try:
|
||||||
get_histories()
|
delete_chat_item()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error: {e}")
|
print(f"Error: {e}")
|
||||||
|
|||||||
@@ -495,8 +495,9 @@ class AsyncChatClient(AsyncFastGPTClient):
|
|||||||
Returns:
|
Returns:
|
||||||
httpx.Response object
|
httpx.Response object
|
||||||
"""
|
"""
|
||||||
params = {"appId": appId, "chatId": chatId, "contentId": contentId}
|
# Try using JSON body first (some APIs prefer this for DELETE)
|
||||||
return await self._send_request("DELETE", "/api/core/chat/item/delete", params=params)
|
data = {"appId": appId, "chatId": chatId, "contentId": contentId}
|
||||||
|
return await self._send_request("DELETE", "/api/core/chat/item/delete", json=data)
|
||||||
|
|
||||||
async def send_feedback(
|
async def send_feedback(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -431,8 +431,9 @@ class ChatClient(FastGPTClient):
|
|||||||
Returns:
|
Returns:
|
||||||
httpx.Response object
|
httpx.Response object
|
||||||
"""
|
"""
|
||||||
params = {"appId": appId, "chatId": chatId, "contentId": contentId}
|
# Try using JSON body first (some APIs prefer this for DELETE)
|
||||||
return self._send_request("DELETE", "/api/core/chat/item/delete", params=params)
|
data = {"appId": appId, "chatId": chatId, "contentId": contentId}
|
||||||
|
return self._send_request("DELETE", "/api/core/chat/item/delete", json=data)
|
||||||
|
|
||||||
def send_feedback(
|
def send_feedback(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -485,7 +485,7 @@ class TestChatClientDeleteChatRecord:
|
|||||||
call_args = mock_send.call_args
|
call_args = mock_send.call_args
|
||||||
assert call_args[0][0] == "DELETE"
|
assert call_args[0][0] == "DELETE"
|
||||||
assert call_args[0][1] == "/api/core/chat/item/delete"
|
assert call_args[0][1] == "/api/core/chat/item/delete"
|
||||||
assert call_args[1]['params'] == {
|
assert call_args[1]['json'] == {
|
||||||
"appId": "app-123",
|
"appId": "app-123",
|
||||||
"chatId": "chat-123",
|
"chatId": "chat-123",
|
||||||
"contentId": "content-123"
|
"contentId": "content-123"
|
||||||
|
|||||||
Reference in New Issue
Block a user