Skip to content

Commit 87d1f80

Browse files
feat: add brain_id and complete brain history openapi test
1 parent 72f09af commit 87d1f80

15 files changed

+711
-17
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ log.txt
88

99

1010

11-
1211
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
1312

1413
# dependencies

backend/core/llm/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class BaseBrainPicking(BaseModel):
2222

2323
# Default class attributes
2424
model: str = None # pyright: ignore reportPrivateUsage=none
25-
temperature: float = 0.0
25+
temperature: float = 0.5
2626
chat_id: str = None # pyright: ignore reportPrivateUsage=none
2727
brain_id: str = None # pyright: ignore reportPrivateUsage=none
2828
max_tokens: int = 256

backend/core/llm/qa_base.py

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ async def wrap_done(fn: Awaitable, event: asyncio.Event):
218218

219219
streamed_chat_history = update_chat_history(
220220
chat_id=self.chat_id,
221+
brain_id=self.brain_id,
221222
user_message=question,
222223
assistant="",
223224
)

backend/core/main.py

+28
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@
2424

2525
logger = get_logger(__name__)
2626

27+
28+
# ## GPTCache
29+
# from gptcache import Cache
30+
# from gptcache.manager.factory import manager_factory
31+
# from gptcache.adapter.api import init_similar_cache
32+
# from gptcache.processor.pre import get_prompt
33+
# from langchain.cache import GPTCache
34+
# import hashlib
35+
# import langchain
36+
# import time
37+
# from langchain.llms import OpenAI
38+
# from gptcache.adapter.langchain_models import LangChainLLMs
39+
40+
41+
# def get_hashed_name(name):
42+
# return hashlib.sha256(name.encode()).hexdigest()
43+
44+
45+
# def init_gptcache(cache_obj: Cache, llm: str):
46+
# hashed_llm = get_hashed_name(llm)
47+
# # init_similar_cache(cache_obj=cache_obj, data_dir=f"similar_cache_{hashed_llm}")
48+
# cache_obj.init(
49+
# pre_embedding_func=get_prompt,
50+
# data_manager=manager_factory()
51+
# )
52+
53+
# langchain.llm_cache = GPTCache(init_gptcache)
54+
2755
sentry_dsn = os.getenv("SENTRY_DSN")
2856
if sentry_dsn:
2957
sentry_sdk.init(

backend/core/models/chat.py

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(self, chat_dict: dict):
2626
@dataclass
2727
class ChatHistory:
2828
chat_id: str
29+
brain_id: str
2930
message_id: str
3031
user_message: str
3132
assistant: str
@@ -35,6 +36,9 @@ def __init__(self, chat_dict: dict):
3536
self.chat_id = chat_dict.get(
3637
"chat_id"
3738
) # pyright: ignore reportPrivateUsage=none
39+
self.brain_id = chat_dict.get(
40+
"brain_id"
41+
) # pyright: ignore reportPrivateUsage=none
3842
self.message_id = chat_dict.get(
3943
"message_id"
4044
) # pyright: ignore reportPrivateUsage=none

backend/core/models/databases/supabase/chats.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ def get_chat_history(self, chat_id: str):
2929

3030
return reponse
3131

32+
def get_brain_history(self, brain_id: str):
33+
reponse = (
34+
self.db.from_("chat_history")
35+
.select("*")
36+
.filter("brain_id", "eq", brain_id)
37+
.order("message_time", desc=False) # Add the ORDER BY clause
38+
.execute()
39+
)
40+
41+
return reponse
42+
3243
def get_user_chats(self, user_id: str):
3344
response = (
3445
self.db.from_("chats")
@@ -38,12 +49,13 @@ def get_user_chats(self, user_id: str):
3849
)
3950
return response
4051

41-
def update_chat_history(self, chat_id: str, user_message: str, assistant: str):
52+
def update_chat_history(self, chat_id: str, brain_id: str, user_message: str, assistant: str):
4253
response = (
4354
self.db.table("chat_history")
4455
.insert(
4556
{
4657
"chat_id": str(chat_id),
58+
"brain_id": str(brain_id),
4759
"user_message": user_message,
4860
"assistant": assistant,
4961
}

backend/core/repository/brain/get_default_user_brain.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def get_user_default_brain(user_id: UUID) -> BrainEntity | None:
1212
supabase_db = get_supabase_db()
1313
brain_id = supabase_db.get_default_user_brain_id(user_id)
1414

15-
logger.info("Default brain response:", brain_id)
15+
# logger.info("Default brain response:", brain_id)
1616

1717
if brain_id is None:
1818
return None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List
2+
3+
from models.chat import ChatHistory
4+
from models.settings import get_supabase_db # For type hinting
5+
6+
7+
def get_brain_history(brain_id: str) -> List[ChatHistory]:
8+
supabase_db = get_supabase_db()
9+
history: List[ChatHistory] = supabase_db.get_brain_history(brain_id).data
10+
if history is None:
11+
return []
12+
else:
13+
return [
14+
ChatHistory(message) # pyright: ignore reportPrivateUsage=none
15+
for message in history
16+
]

backend/core/repository/chat/update_chat_history.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
from models.settings import get_supabase_db
66

77

8-
def update_chat_history(chat_id: str, user_message: str, assistant: str) -> ChatHistory:
8+
def update_chat_history(chat_id: str, brain_id: str, user_message: str, assistant: str) -> ChatHistory:
99
supabase_db = get_supabase_db()
1010
response: List[ChatHistory] = (
11-
supabase_db.update_chat_history(chat_id, user_message, assistant)
11+
supabase_db.update_chat_history(chat_id, brain_id, user_message, assistant)
1212
).data
1313
if len(response) == 0:
1414
raise HTTPException(

backend/core/routes/chat_routes.py

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from repository.chat.create_chat import CreateChatProperties, create_chat
2222
from repository.chat.get_chat_by_id import get_chat_by_id
2323
from repository.chat.get_chat_history import get_chat_history
24+
from repository.chat.get_brain_history import get_brain_history
2425
from repository.chat.get_user_chats import get_user_chats
2526
from repository.chat.update_chat import ChatUpdatableProperties, update_chat
2627
from repository.user_identity.get_user_identity import get_user_identity
@@ -295,3 +296,13 @@ async def get_chat_history_handler(
295296
) -> List[ChatHistory]:
296297
# TODO: RBAC with current_user
297298
return get_chat_history(chat_id) # pyright: ignore reportPrivateUsage=none
299+
300+
# get brain history
301+
@chat_router.get(
302+
"/chat/{brain_id}/brain_history", dependencies=[Depends(AuthBearer())], tags=["Chat"]
303+
)
304+
async def get_brain_history_handler(
305+
brain_id: UUID,
306+
) -> List[ChatHistory]:
307+
# TODO: RBAC with current_user
308+
return get_brain_history(brain_id) # pyright: ignore reportPrivateUsage=none

migration.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ if $prompt_for_db_info ; then
3838
fi
3939

4040
# Ask user whether to create tables or run migrations
41-
CHOICE=$(gum choose --header "Choose an option" "Create all tables" "Run Migrations")
41+
# CHOICE=$(gum choose --header "Choose an option" "Create all tables" "Run Migrations")
42+
CHOICE="Create all tables"
4243

4344
if [ "$CHOICE" == "Create all tables" ]; then
4445
# Running the tables.sql file to create tables

openapi-brain.yaml

+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
---
2+
openapi: "3.0.1"
3+
info:
4+
title: "MyGPT API test"
5+
description: "This is openapi file for mygpt apis."
6+
version: 3.0.1
7+
servers:
8+
- url: "http://walletgpt.info:5050"
9+
tags:
10+
- name: brain
11+
paths:
12+
/brains/:
13+
post:
14+
tags:
15+
- brain
16+
parameters: []
17+
requestBody:
18+
content:
19+
application/json:
20+
schema:
21+
$ref: '#/components/schemas/CreateBrainProperties'
22+
responses:
23+
"200":
24+
description: "Generated by SwaggerHub Explore"
25+
security:
26+
- TokenCredentials: []
27+
/upload:
28+
post:
29+
tags:
30+
- upload
31+
parameters:
32+
- in: query
33+
name: brain_id
34+
required: true
35+
description: ID for the brain
36+
schema:
37+
type: string
38+
default: "780dc579-49a2-4b2f-82d0-5cca26515179"
39+
requestBody:
40+
required: true
41+
content:
42+
multipart/form-data:
43+
schema:
44+
type: object
45+
properties:
46+
uploadFile:
47+
type: string
48+
format: binary
49+
responses:
50+
"200":
51+
description: "Generated by SwaggerHub Explore"
52+
security:
53+
- TokenCredentials: []
54+
/crawl:
55+
post:
56+
tags:
57+
- crawl
58+
parameters:
59+
- in: query
60+
name: brain_id
61+
required: true
62+
description: ID for the brain
63+
schema:
64+
type: string
65+
default: "780dc579-49a2-4b2f-82d0-5cca26515179"
66+
requestBody:
67+
required: true
68+
content:
69+
application/json:
70+
schema:
71+
$ref: '#/components/schemas/CrawlWebsite'
72+
responses:
73+
"200":
74+
description: "Generated by SwaggerHub Explore"
75+
security:
76+
- TokenCredentials: []
77+
/chat/{chat_id}/question/stream:
78+
post:
79+
tags:
80+
- chat
81+
parameters:
82+
- name: chat_id
83+
in: path
84+
required: true
85+
description: ID of the chat
86+
schema:
87+
type: string
88+
default: "021a43da-22cd-4218-a037-5f2f1ac43056"
89+
- name: brain_id
90+
in: query
91+
required: true
92+
description: ID for the brain
93+
schema:
94+
type: string
95+
default: "780dc579-49a2-4b2f-82d0-5cca26515179"
96+
requestBody:
97+
required: true
98+
content:
99+
application/json:
100+
schema:
101+
type: object
102+
properties:
103+
question:
104+
type: string
105+
default: "What is python?"
106+
responses:
107+
"200":
108+
description: "Generated by SwaggerHub Explore"
109+
content:
110+
application/json:
111+
schema:
112+
$ref: '#/components/schemas/ChatHistoryList'
113+
security:
114+
- TokenCredentials: []
115+
/chat/{chat_id}/history:
116+
get:
117+
tags:
118+
- chat
119+
parameters:
120+
- name: chat_id
121+
in: path
122+
required: true
123+
description: ID of the chat
124+
schema:
125+
type: string
126+
default: "a9e343fb-f37c-449f-ab68-e51332c498e8"
127+
responses:
128+
"200":
129+
description: "Generated by SwaggerHub Explore"
130+
security:
131+
- TokenCredentials: []
132+
/chat/{brain_id}/brain_history:
133+
get:
134+
tags:
135+
- chat
136+
parameters:
137+
- name: brain_id
138+
in: path
139+
required: true
140+
description: ID of the brain
141+
schema:
142+
type: string
143+
default: "780dc579-49a2-4b2f-82d0-5cca26515179"
144+
responses:
145+
"200":
146+
description: "Generated by SwaggerHub Explore"
147+
security:
148+
- TokenCredentials: []
149+
components:
150+
schemas:
151+
CreateBrainProperties:
152+
type: object
153+
properties:
154+
description:
155+
type: string
156+
default: "Swagger API test"
157+
name:
158+
type: string
159+
default: "Default swagger brain"
160+
model:
161+
type: string
162+
default: "gpt-3.5-turbo-0613"
163+
max_tokens:
164+
type: integer
165+
default: 378
166+
openai_api_key:
167+
type: string
168+
default: "sk-"
169+
temperature:
170+
type: string
171+
default: "0.75"
172+
CrawlWebsite:
173+
type: object
174+
properties:
175+
depth:
176+
type: integer
177+
default: 1
178+
js:
179+
type: boolean
180+
default: false
181+
max_pages:
182+
type: integer
183+
default: 100
184+
max_time:
185+
type: integer
186+
default: 60
187+
url:
188+
type: string
189+
default: "https://medium.com/codingthesmartway-com-blog/quivr-the-open-source-ai-project-thats-your-second-brain-in-the-cloud-17e3541fe45c"
190+
ChatHistory:
191+
type: object
192+
properties:
193+
chat_id:
194+
type: string
195+
message_id:
196+
type: string
197+
user_message:
198+
type: string
199+
assistant:
200+
type: string
201+
message_time:
202+
type: string
203+
ChatHistoryList:
204+
type: array
205+
items:
206+
$ref: '#/components/schemas/ChatHistory'
207+
securitySchemes:
208+
TokenCredentials:
209+
type: "http"
210+
scheme: "bearer"

0 commit comments

Comments
 (0)