Skip to content

Commit 409a158

Browse files
committed
DEPLOYMENT DEV BY ANH TU
1 parent d907243 commit 409a158

29 files changed

+6704
-21
lines changed

README.md

9.06 KB

chatbot-ai-langchain

Build Chat AI | 8 Sync Dev

Yêu cầu

Giới Thiệu

1. Ollama

  • Ollama là một công cụ được thiết kế để hỗ trợ xây dựng và triển khai các mô hình học sâu.
  • Ứng dụng trong dự án: Ollama có thể được sử dụng để phát triển các mô hình dự đoán hoặc phân loại, đặc biệt hữu ích trong việc xây dựng các mô hình AI hoặc chatbot có khả năng học và tương tác thông minh với người dùng.

2. Solara

  • Solara là một framework xây dựng ứng dụng web tương tác mạnh mẽ, cho phép phát triển giao diện người dùng tương tác cao mà không cần nhiều mã JavaScript.
  • Ứng dụng trong dự án: Trong dự án của bạn, Solara có thể được sử dụng để xây dựng giao diện người dùng (UI) cho các thành phần như chat, bảng điều khiển, hoặc các trang web, giúp người dùng tương tác với các mô hình AI một cách trực quan và dễ dàng.

3. LangChain

  • LangChain là một framework nhằm tạo ra các hệ thống sử dụng ngôn ngữ tự nhiên để tương tác và thao tác với dữ liệu, đặc biệt hiệu quả trong việc xử lý ngôn ngữ tự nhiên (NLP).
  • Ứng dụng trong dự án: LangChain có thể được tích hợp để xây dựng các chức năng xử lý ngôn ngữ tự nhiên cho chatbot hoặc các hệ thống phân tích văn bản trong dự án, giúp nâng cao khả năng tương tác và hiểu biết của hệ thống.

Cách tích hợp vào dự án

  • Ollama có thể được dùng để phát triển các mô hình AI cốt lõi.
  • Solara sẽ đóng vai trò là giao diện người dùng, kết nối giữa người dùng và mô hình AI, cung cấp trải nghiệm tương tác.
  • LangChain hỗ trợ việc hiểu và xử lý các câu lệnh ngôn ngữ tự nhiên, giúp cải thiện trải nghiệm người dùng khi tương tác với hệ thống AI.

Cả ba công cụ này đều có thể kết hợp chặt chẽ trong một dự án để tạo ra một hệ thống AI hoàn chỉnh với khả năng giao tiếp tự nhiên và giao diện người dùng mạnh mẽ.


Cấu trúc thư mục dự án

Dự án của bạn được tổ chức thành các thư mục và tệp như sau:

  1. Thư mục app/:

    • Chứa cấu trúc chính của ứng dụng.
    • Thư mục này hiện tại chỉ chứa thư mục con là __pycache__, nơi lưu trữ các tệp biên dịch Python (.pyc) sau khi thực thi mã.
  2. Thư mục components/:

    • Chứa các thành phần (components) tái sử dụng trong ứng dụng.
    • Thư mục __pycache__/: Lưu trữ tệp biên dịch Python.
    • Tệp __init__.py: Xác định thư mục components là một module Python.
    • Tệp show_chat.py: Có thể là một component xử lý chức năng liên quan đến hiển thị hoặc tương tác với chat.
  3. Thư mục layouts/:

    • Chứa các bố cục (layouts) dùng cho các trang hoặc thành phần khác.
    • Thư mục __pycache__/: Lưu trữ tệp biên dịch Python.
    • Tệp __init__.py: Xác định thư mục layouts là một module Python.
    • Tệp base_layout.py: Có thể chứa bố cục cơ bản dùng làm nền tảng cho các bố cục khác.
  4. Thư mục pages/:

    • Chứa các trang chính của ứng dụng.
    • Thư mục __pycache__/: Lưu trữ tệp biên dịch Python.
    • Tệp __init__.py: Xác định thư mục pages là một module Python.
    • Tệp home_page.py: Có thể là trang chính hoặc trang chủ của ứng dụng.
    • Tệp router.py: Có thể xử lý điều hướng giữa các trang.
    • Tệp setting.py: Có thể chứa các cài đặt hoặc cấu hình của trang.
  5. Thư mục board/:

    • Có thể chứa các thành phần hoặc logic liên quan đến "bảng" (board), nhưng hiện tại thư mục này trống.
  6. Thư mục doc/:

    • Chứa tài liệu của dự án.
    • Tệp doc.mddoc.pdf: Tệp tài liệu hướng dẫn, mô tả, hoặc báo cáo.
  7. Thư mục venv/:

    • Đây là thư mục môi trường ảo của Python, nơi chứa các thư viện và gói được cài đặt mà không ảnh hưởng đến môi trường Python toàn cục.
  8. Các tệp khác:

    • .gitignore: Danh sách các tệp và thư mục cần bỏ qua khi đẩy mã nguồn lên Git.
    • main.py: Có thể là tệp chính để khởi động ứng dụng.
    • README.md: Tệp tài liệu cung cấp thông tin tổng quan về dự án.
    • requirements.txt: Danh sách các thư viện Python cần thiết để chạy ứng dụng, đã được chỉnh sửa gần đây.

Mục đích

  • Cấu trúc này giúp phân chia rõ ràng các phần chức năng của ứng dụng, dễ bảo trì và phát triển.
  • Mỗi thư mục/module có thể đại diện cho một phần của ứng dụng, từ các component, bố cục, trang cho đến tài liệu.

README.pdf

479 KB
Binary file not shown.

app.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

app/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from app.router import ROUTES
2+
import solara
3+
4+
5+
6+
def get_all_routes():
7+
routes = []
8+
for route in ROUTES:
9+
routes.append(solara.Route(**route))
10+
return routes
11+
12+
routes = get_all_routes()
591 Bytes
Binary file not shown.
394 Bytes
Binary file not shown.

app/components/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from app.components.show_chat import show_chat
239 Bytes
Binary file not shown.
Binary file not shown.

app/components/show_chat.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import solara
2+
from solara.tasks import use_task, Task
3+
import solara.lab as lab
4+
from langchain_ollama.llms import OllamaLLM
5+
from asgiref.sync import sync_to_async
6+
7+
8+
manager = solara.reactive([])
9+
10+
@solara.component
11+
def get_ai_chat(chain: OllamaLLM, current_mess: str):
12+
print('current_mess', current_mess)
13+
if current_mess == '':
14+
manager.set(manager.get() + [{
15+
'user': current_mess,
16+
'ai': 'Please enter a message',
17+
}])
18+
return lab.ChatMessage(children=[solara.Markdown(md_text='Please enter a message')], user=False)
19+
20+
ai_mess = chain.invoke(current_mess)
21+
manager.set(manager.get() + [{
22+
'user': current_mess,
23+
'ai': '...' if ai_mess == '' else ai_mess,
24+
}])
25+
return lab.ChatMessage(children=[
26+
solara.Markdown(md_text=ai_mess),
27+
], user=False)
28+
29+
# async def async_get_ai_chat(chain: OllamaLLM, current_mess: str):
30+
# return await sync_to_async(get_ai_chat)(chain, current_mess)
31+
32+
def get_old_mess():
33+
old_mess = []
34+
for mess in manager.get():
35+
user_chat = lab.ChatMessage(
36+
children=[
37+
solara.Markdown(md_text=mess['user']),
38+
],
39+
user=True,
40+
)
41+
ai_chat = lab.ChatMessage(
42+
children=[
43+
solara.Markdown(md_text=mess['ai']),
44+
],
45+
user=False,
46+
) if mess['ai'] != '' else solara.Div()
47+
old_mess.extend([user_chat, ai_chat])
48+
return old_mess
49+
50+
@solara.component
51+
def show_chat(
52+
mess_reactive: solara.Reactive,
53+
config_model: dict,
54+
list_messages_user_reactive: solara.Reactive = None,
55+
list_messages_chatbox_reactive: solara.Reactive = None,
56+
):
57+
chain = config_model['prompt'] | config_model['model']
58+
get_ai_chat_response: Task = use_task(
59+
lambda: get_ai_chat(chain=chain, current_mess=mess_reactive.value),
60+
dependencies=[mess_reactive.value]
61+
)
62+
63+
if get_ai_chat_response.finished:
64+
old_mess = get_old_mess()[:-1]
65+
return lab.ChatBox(children=[
66+
*old_mess,
67+
get_ai_chat_response.result.value,
68+
])
69+
else:
70+
old_mess = get_old_mess()
71+
return lab.ChatBox(children=[
72+
*old_mess,
73+
lab.ChatMessage(children=[mess_reactive.value], user=True),
74+
solara.ProgressLinear(value=get_ai_chat_response.progress),
75+
])
76+
77+
78+
79+
80+
81+
82+
83+

app/layouts/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from app.layouts.base_layout import base_layout
237 Bytes
Binary file not shown.
Binary file not shown.

app/layouts/base_layout.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import solara
2+
import solara.lab as lab
3+
4+
5+
@solara.component
6+
def base_layout(children: list):
7+
with solara.AppLayout() as layout:
8+
with solara.AppBar() as appbar:
9+
lab.ThemeToggle()
10+
11+
with solara.Sidebar() as sidebar:
12+
...
13+
14+
with solara.Div() as content:
15+
content.add_children(children)
16+
17+
return layout

app/pages/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from app.pages.home_page import home_page
229 Bytes
Binary file not shown.
2.81 KB
Binary file not shown.

app/pages/home_page.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import solara
2+
import solara.lab as lab
3+
4+
from app.components import (
5+
show_chat,
6+
)
7+
8+
9+
from langchain_core.prompts import ChatPromptTemplate
10+
from langchain_ollama.llms import OllamaLLM
11+
12+
template = """Question: {question}
13+
14+
Answer: Let's think step by step."""
15+
16+
promt = ChatPromptTemplate.from_template(template)
17+
18+
model = OllamaLLM(model='llama3')
19+
20+
21+
22+
23+
message = solara.reactive('')
24+
list_messages_user = solara.reactive([])
25+
list_messages_chatbox = solara.reactive([])
26+
27+
def send_message(mess: str):
28+
message.set(mess)
29+
list_messages_user.set(list_messages_user.get() + [mess])
30+
# print(message.value, list_messages_user.value)
31+
32+
33+
@solara.component
34+
def home_page():
35+
with solara.Div(
36+
style={
37+
'height': '100vh',
38+
}
39+
) as container:
40+
solara.Text('Build Chatbot Ollama Model With 8 Sync Dev', style={'font-size': '2rem', 'margin-bottom': '20px'})
41+
with solara.Div(
42+
style={
43+
'height': '60%',
44+
'border-bottom': '1px solid #ccc',
45+
'border-radius': '5px',
46+
'shadow': '0 0 10px rgba(0, 0, 0, 0.1)',
47+
'overflow-y': 'auto',
48+
}
49+
) as showchat:
50+
show_chat(
51+
mess_reactive=message,
52+
config_model={
53+
'model': model,
54+
'prompt': promt,
55+
},
56+
)
57+
with solara.Div(
58+
style={
59+
'padding': '10px',
60+
}
61+
) as chat_box:
62+
lab.ChatInput(send_callback=send_message)

app/router.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from app.pages import (
2+
home_page,
3+
)
4+
5+
from app.layouts import (
6+
base_layout,
7+
)
8+
9+
10+
ROUTES = [
11+
{
12+
'path': '/',
13+
'component': home_page,
14+
'label': 'Home',
15+
'layout': base_layout,
16+
}
17+
]

app/setting.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from pathlib import Path
2+
3+
BASE_DIR = Path(__file__).resolve().parent.parent

0 commit comments

Comments
 (0)