Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pamella committed Jun 21, 2024
1 parent a3d4dd4 commit e34f4ee
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 14 deletions.
5 changes: 3 additions & 2 deletions django_ai_assistant/api/schemas.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
from typing import Union

from django.utils import timezone

Expand Down Expand Up @@ -29,7 +30,7 @@ class ThreadSchemaIn(Schema):

class ThreadMessagesSchemaIn(Schema):
assistant_id: str
content: str
content: Union[str, list[Union[str, dict]]]


class ThreadMessageTypeEnum(str, Enum):
Expand All @@ -44,4 +45,4 @@ class ThreadMessageTypeEnum(str, Enum):
class ThreadMessagesSchemaOut(Schema):
id: str # noqa: A003
type: ThreadMessageTypeEnum # noqa: A003
content: str
content: Union[str, list[Union[str, dict]]]
31 changes: 30 additions & 1 deletion django_ai_assistant/helpers/use_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,37 @@ def create_message(

# TODO: Check if we can separate the message creation from the chain invoke
assistant = assistant_cls(user=user, request=request)
print("\n\ncontent", content, "\n\n")
# assistant_message = assistant.invoke(
# {"input": content},
# thread_id=thread.id,
# )
assistant_message = assistant.invoke(
{"input": content},
# {
# "input": HumanMessage(
# content=[
# content,
# "describe the weather",
# # {
# # "type": "image_url",
# # "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Altja_j%C3%B5gi_Lahemaal.jpg/320px-Altja_j%C3%B5gi_Lahemaal.jpg",
# # },
# ],
# )
# },
# [HumanMessage(content=content), HumanMessage(content="describe the weather")],
[
HumanMessage(
content=[
content,
"describe the weather",
# {
# "type": "image_url",
# "image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Altja_j%C3%B5gi_Lahemaal.jpg/320px-Altja_j%C3%B5gi_Lahemaal.jpg",
# },
],
)
],
thread_id=thread.id,
)
return assistant_message
Expand Down
8 changes: 7 additions & 1 deletion example/assets/js/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ function ChatMessage({
</Tooltip>
);

console.log({ message });

return (
<Group
gap="lg"
Expand All @@ -79,7 +81,11 @@ function ChatMessage({
bg="var(--mantine-color-gray-0)"
>
<Group gap="md" justify="space-between" align="top">
<Markdown className={classes.mdMessage}>{message.content}</Markdown>
<Markdown className={classes.mdMessage}>
{Array.isArray(message.content)
? message.content[0]
: message.content}
</Markdown>
</Group>
</Paper>

Expand Down
42 changes: 38 additions & 4 deletions frontend/openapi_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,25 @@
"$ref": "#/components/schemas/ThreadMessageTypeEnum"
},
"content": {
"title": "Content",
"type": "string"
"anyOf": [
{
"type": "string"
},
{
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "object"
}
]
},
"type": "array"
}
],
"title": "Content"
}
},
"required": [
Expand All @@ -453,8 +470,25 @@
"type": "string"
},
"content": {
"title": "Content",
"type": "string"
"anyOf": [
{
"type": "string"
},
{
"items": {
"anyOf": [
{
"type": "string"
},
{
"type": "object"
}
]
},
"type": "array"
}
],
"title": "Content"
}
},
"required": [
Expand Down
42 changes: 38 additions & 4 deletions frontend/src/client/schemas.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,25 @@ export const $ThreadMessagesSchemaOut = {
'$ref': '#/components/schemas/ThreadMessageTypeEnum'
},
content: {
title: 'Content',
type: 'string'
anyOf: [
{
type: 'string'
},
{
items: {
anyOf: [
{
type: 'string'
},
{
type: 'object'
}
]
},
type: 'array'
}
],
title: 'Content'
}
},
required: ['id', 'type', 'content'],
Expand All @@ -100,8 +117,25 @@ export const $ThreadMessagesSchemaIn = {
type: 'string'
},
content: {
title: 'Content',
type: 'string'
anyOf: [
{
type: 'string'
},
{
items: {
anyOf: [
{
type: 'string'
},
{
type: 'object'
}
]
},
type: 'array'
}
],
title: 'Content'
}
},
required: ['assistant_id', 'content'],
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/client/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ export type ThreadMessageTypeEnum = 'human' | 'ai' | 'generic' | 'system' | 'fun
export type ThreadMessagesSchemaOut = {
id: string;
type: ThreadMessageTypeEnum;
content: string;
content: string | Array<(string | {
[key: string]: unknown;
})>;
};

export type ThreadMessagesSchemaIn = {
assistant_id: string;
content: string;
content: string | Array<(string | {
[key: string]: unknown;
})>;
};

export type DjangoAiAssistantListAssistantsResponse = Array<AssistantSchema>;
Expand Down

0 comments on commit e34f4ee

Please sign in to comment.