Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user_id for third-party system to record sessions. #4206

Merged
merged 8 commits into from
Dec 24, 2024
8 changes: 5 additions & 3 deletions api/apps/sdk/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def create(tenant_id, chat_id):
"id": get_uuid(),
"dialog_id": req["dialog_id"],
"name": req.get("name", "New session"),
"message": [{"role": "assistant", "content": dia[0].prompt_config.get("prologue")}]
"message": [{"role": "assistant", "content": dia[0].prompt_config.get("prologue")}],
"user_id": req.get("user_id", "")
}
if not conv.get("name"):
return get_error_data_result(message="`name` can not be empty.")
Expand All @@ -71,7 +72,7 @@ def create_agent_session(tenant_id, agent_id):
return get_error_data_result("Agent not found.")

if not UserCanvasService.query(user_id=tenant_id,id=agent_id):
return get_error_data_result("You cannot access the agent.")
return get_error_data_result("You cannot access the agent.")

if not isinstance(cvs.dsl, str):
cvs.dsl = json.dumps(cvs.dsl, ensure_ascii=False)
Expand Down Expand Up @@ -197,11 +198,12 @@ def list_session(tenant_id, chat_id):
page_number = int(request.args.get("page", 1))
items_per_page = int(request.args.get("page_size", 30))
orderby = request.args.get("orderby", "create_time")
user_id = request.args.get("user_id")
if request.args.get("desc") == "False" or request.args.get("desc") == "false":
desc = False
else:
desc = True
convs = ConversationService.get_list(chat_id, page_number, items_per_page, orderby, desc, id, name)
convs = ConversationService.get_list(chat_id, page_number, items_per_page, orderby, desc, id, name, user_id)
if not convs:
return get_result(data=[])
for conv in convs:
Expand Down
13 changes: 10 additions & 3 deletions api/db/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
)
from playhouse.pool import PooledMySQLDatabase, PooledPostgresqlDatabase


from api.db import SerializedType, ParserType
from api import settings
from api import utils
Expand Down Expand Up @@ -926,6 +925,7 @@ class Conversation(DataBaseModel):
name = CharField(max_length=255, null=True, help_text="converastion name", index=True)
message = JSONField(null=True)
reference = JSONField(null=True, default=[])
user_id = CharField(max_length=255, null=True, help_text="user_id", index=True)

class Meta:
db_table = "conversation"
Expand Down Expand Up @@ -1070,13 +1070,13 @@ def migrate_db():
pass
try:
migrate(
migrator.add_column("tenant_llm","max_tokens",IntegerField(default=8192,index=True))
migrator.add_column("tenant_llm", "max_tokens", IntegerField(default=8192, index=True))
)
except Exception:
pass
try:
migrate(
migrator.add_column("api_4_conversation","dsl",JSONField(null=True, default={}))
migrator.add_column("api_4_conversation", "dsl", JSONField(null=True, default={}))
)
except Exception:
pass
Expand Down Expand Up @@ -1105,3 +1105,10 @@ def migrate_db():
)
except Exception:
pass
try:
migrate(
migrator.add_column("conversation", "user_id",
CharField(max_length=255, null=True, help_text="user_id", index=True))
)
except Exception:
pass
9 changes: 6 additions & 3 deletions api/db/services/api_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ class API4ConversationService(CommonService):

@classmethod
@DB.connection_context()
def get_list(cls,dialog_id, tenant_id,
page_number, items_per_page, orderby, desc, id):
sessions = cls.model.select().where(cls.model.dialog_id ==dialog_id)
def get_list(cls, dialog_id, tenant_id,
page_number, items_per_page,
orderby, desc, id, user_id=None):
sessions = cls.model.select().where(cls.model.dialog_id == dialog_id)
if id:
sessions = sessions.where(cls.model.id == id)
if user_id:
sessions = sessions.where(cls.model.user_id == user_id)
if desc:
sessions = sessions.order_by(cls.model.getter_by(orderby).desc())
else:
Expand Down
29 changes: 16 additions & 13 deletions api/db/services/conversation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ class ConversationService(CommonService):

@classmethod
@DB.connection_context()
def get_list(cls,dialog_id,page_number, items_per_page, orderby, desc, id , name):
sessions = cls.model.select().where(cls.model.dialog_id ==dialog_id)
def get_list(cls, dialog_id, page_number, items_per_page, orderby, desc, id, name, user_id=None):
sessions = cls.model.select().where(cls.model.dialog_id == dialog_id)
if id:
sessions = sessions.where(cls.model.id == id)
if name:
sessions = sessions.where(cls.model.name == name)
if user_id:
sessions = sessions.where(cls.model.user_id == user_id)
if desc:
sessions = sessions.order_by(cls.model.getter_by(orderby).desc())
else:
Expand All @@ -52,15 +54,16 @@ def structure_answer(conv, ans, message_id, session_id):

def get_value(d, k1, k2):
return d.get(k1, d.get(k2))

chunk_list = [{
"id": get_value(chunk, "chunk_id", "id"),
"content": get_value(chunk, "content", "content_with_weight"),
"document_id": get_value(chunk, "doc_id", "document_id"),
"document_name": get_value(chunk, "docnm_kwd", "document_name"),
"dataset_id": get_value(chunk, "kb_id", "dataset_id"),
"image_id": get_value(chunk, "image_id", "img_id"),
"positions": get_value(chunk, "positions", "position_int"),
} for chunk in reference.get("chunks", [])]
"id": get_value(chunk, "chunk_id", "id"),
"content": get_value(chunk, "content", "content_with_weight"),
"document_id": get_value(chunk, "doc_id", "document_id"),
"document_name": get_value(chunk, "docnm_kwd", "document_name"),
"dataset_id": get_value(chunk, "kb_id", "dataset_id"),
"image_id": get_value(chunk, "image_id", "img_id"),
"positions": get_value(chunk, "positions", "position_int"),
} for chunk in reference.get("chunks", [])]

reference["chunks"] = chunk_list
ans["id"] = message_id
Expand Down Expand Up @@ -88,10 +91,11 @@ def completion(tenant_id, chat_id, question, name="New session", session_id=None
if not session_id:
session_id = get_uuid()
conv = {
"id":session_id ,
"id": session_id,
"dialog_id": chat_id,
"name": name,
"message": [{"role": "assistant", "content": dia[0].prompt_config.get("prologue")}]
"message": [{"role": "assistant", "content": dia[0].prompt_config.get("prologue")}],
"user_id": kwargs.get("user_id", "")
}
ConversationService.save(**conv)
yield "data:" + json.dumps({"code": 0, "message": "",
Expand Down Expand Up @@ -226,4 +230,3 @@ def iframe_completion(dialog_id, question, session_id=None, stream=True, **kwarg
API4ConversationService.append_message(conv.id, conv.to_dict())
break
yield answer

28 changes: 23 additions & 5 deletions docs/references/http_api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,7 @@ Creates a session with a chat assistant.
- `'Authorization: Bearer <YOUR_API_KEY>'`
- Body:
- `"name"`: `string`
- `"user_id"`: `string`(optional)

##### Request example

Expand All @@ -1741,6 +1742,8 @@ curl --request POST \
The ID of the associated chat assistant.
- `"name"`: (*Body parameter*), `string`
The name of the chat session to create.
- `"user_id"`: (*Body parameter*), `string`
The user ID of third-party upstream system which invokes the API. It's optional.
KevinHuSh marked this conversation as resolved.
Show resolved Hide resolved

#### Response

Expand Down Expand Up @@ -1793,6 +1796,7 @@ Updates a session of a specified chat assistant.
- `'Authorization: Bearer <YOUR_API_KEY>'`
- Body:
- `"name`: `string`
- `"user_id`: `string`(optional)

##### Request example

Expand All @@ -1815,6 +1819,8 @@ curl --request PUT \
The ID of the session to update.
- `"name"`: (*Body Parameter*), `string`
The revised name of the session.
- `"user_id"`: (*Body parameter*), `string`
The user ID of third-party upstream system which invokes the API. It's optional.
KevinHuSh marked this conversation as resolved.
Show resolved Hide resolved

#### Response

Expand Down Expand Up @@ -1846,7 +1852,7 @@ Lists sessions associated with a specified chat assistant.
#### Request

- Method: GET
- URL: `/api/v1/chats/{chat_id}/sessions?page={page}&page_size={page_size}&orderby={orderby}&desc={desc}&name={session_name}&id={session_id}`
- URL: `/api/v1/chats/{chat_id}/sessions?page={page}&page_size={page_size}&orderby={orderby}&desc={desc}&name={session_name}&id={session_id}&user_id={user_id}`
- Headers:
- `'Authorization: Bearer <YOUR_API_KEY>'`

Expand Down Expand Up @@ -1876,6 +1882,8 @@ curl --request GET \
The name of the chat session to retrieve.
- `id`: (*Filter parameter*), `string`
The ID of the chat session to retrieve.
- `user_id`: (*Filter parameter*), `string`
The user ID of third-party upstream system which updates this field by creating session.
KevinHuSh marked this conversation as resolved.
Show resolved Hide resolved

#### Response

Expand Down Expand Up @@ -2000,7 +2008,8 @@ Asks a specified chat assistant a question to start an AI-powered conversation.
- Body:
- `"question"`: `string`
- `"stream"`: `boolean`
- `"session_id"`: `string`
- `"session_id"`: `string`(optional)
- `"user_id`: `string`(optional)

##### Request example

Expand Down Expand Up @@ -2038,6 +2047,8 @@ curl --request POST \
- `false`: Disable streaming.
- `"session_id"`: (*Body Parameter*)
The ID of session. If it is not provided, a new session will be generated.
- `"user_id"`: (*Body parameter*), `string`
The user ID of third-party upstream system which invokes the API. It's optional and only needed when no session_id provided.
KevinHuSh marked this conversation as resolved.
Show resolved Hide resolved

#### Response

Expand Down Expand Up @@ -2162,6 +2173,8 @@ Creates a session with an agent.
- Body:
- the required parameters:`str`
- the optional parameters:`str`
- `"user_id"`: `string`
The user ID of third-party upstream system which invokes the API. It's optional.
KevinHuSh marked this conversation as resolved.
Show resolved Hide resolved

##### Request example
If `begin` component in the agent doesn't have required parameters:
Expand Down Expand Up @@ -2337,6 +2350,7 @@ Asks a specified agent a question to start an AI-powered conversation.
- `"question"`: `string`
- `"stream"`: `boolean`
- `"session_id"`: `string`
- `"user_id"`: `string`(optional)
- other parameters: `string`
##### Request example
If the `begin` component doesn't have parameters, the following code will create a session.
Expand Down Expand Up @@ -2388,6 +2402,8 @@ curl --request POST \
- `false`: Disable streaming.
- `"session_id"`: (*Body Parameter*)
The ID of the session. If it is not provided, a new session will be generated.
- `"user_id"`: (*Body parameter*), `string`
The user ID of third-party upstream system which invokes the API. It's optional and only needed when no session_id provided.
KevinHuSh marked this conversation as resolved.
Show resolved Hide resolved
- Other parameters: (*Body Parameter*)
The parameters in the begin component.
#### Response
Expand Down Expand Up @@ -2538,7 +2554,7 @@ Failure:

### List agent sessions

**GET** `/api/v1/agents/{agent_id}/sessions?page={page}&page_size={page_size}&orderby={orderby}&desc={desc}&id={session_id}`
**GET** `/api/v1/agents/{agent_id}/sessions?page={page}&page_size={page_size}&orderby={orderby}&desc={desc}&id={session_id}&user_id={user_id}`

Lists sessions associated with a specified agent.

Expand All @@ -2553,7 +2569,7 @@ Lists sessions associated with a specified agent.

```bash
curl --request GET \
--url http://{address}/api/v1/agents/{agent_id}/sessions?page={page}&page_size={page_size}&orderby={orderby}&desc={desc}&id={session_id} \
--url http://{address}/api/v1/agents/{agent_id}/sessions?page={page}&page_size={page_size}&orderby={orderby}&desc={desc}&id={session_id}&user_id={user_id} \
--header 'Authorization: Bearer <YOUR_API_KEY>'
```

Expand All @@ -2573,7 +2589,9 @@ curl --request GET \
Indicates whether the retrieved sessions should be sorted in descending order. Defaults to `true`.
- `id`: (*Filter parameter*), `string`
The ID of the agent session to retrieve.

- `user_id`: (*Filter parameter*), `string`
The user ID of third-party upstream system which updates this field by creating session.
KevinHuSh marked this conversation as resolved.
Show resolved Hide resolved

#### Response

Success:
Expand Down