Skip to content

Commit d17b61b

Browse files
Upgrade examples to langchain 1.0 (zauberzeug#5304)
### Motivation Pipeline's red due to langchain 1.0 incompatibilities. Error line: `ModuleNotFoundError: No module named 'langchain.callbacks'`, since they moved it in 1.0. ### Implementation 1. Do not install langchain in "install packages to run the examples". Installed in test_startup.sh anyways. 2. Bump the version to avoid installing too old langchain 3. Change the code (Falko's work) ### Progress - [x] I chose a meaningful title that completes the sentence: "If applied, this PR will..." - [x] The implementation is complete. - [x] Pytests have been added (or are not necessary). - [x] Documentation has been added (or is not necessary). https://github.com/evnchn/nicegui/actions/runs/18641263733 Of course it is more than welcome to make langchain 1.0 compatible, but I don't have OpenAI access... --------- Co-authored-by: Falko Schindler <[email protected]>
1 parent 2fb9f9d commit d17b61b

File tree

4 files changed

+8
-71
lines changed

4 files changed

+8
-71
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
run: |
6161
set -x
6262
# install packages to run the examples
63-
poetry run pip install opencv-python opencv-contrib-python-headless httpx isort replicate langchain openai simpy tortoise-orm
63+
poetry run pip install opencv-python opencv-contrib-python-headless httpx isort replicate openai simpy tortoise-orm
6464
- name: test startup
6565
run: poetry run ./test_startup.sh
6666
- name: restore dependencies for effective caching

examples/chat_with_ai/log_callback_handler.py

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

examples/chat_with_ai/main.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#!/usr/bin/env python3
22
from html_sanitizer import Sanitizer
33
from langchain_openai import ChatOpenAI
4-
from log_callback_handler import NiceGuiLogElementCallbackHandler
54

65
from nicegui import ui
76

87
OPENAI_API_KEY = 'not-set' # TODO: set your OpenAI API key here
98

109

11-
@ui.page('/')
12-
def main():
10+
def root():
1311
llm = ChatOpenAI(model_name='gpt-4o-mini', streaming=True, openai_api_key=OPENAI_API_KEY)
1412

1513
async def send() -> None:
@@ -22,27 +20,15 @@ async def send() -> None:
2220
spinner = ui.spinner(type='dots')
2321

2422
response = ''
25-
async for chunk in llm.astream(question, config={'callbacks': [NiceGuiLogElementCallbackHandler(log)]}):
23+
async for chunk in llm.astream(question):
2624
response += chunk.content
2725
response_message.clear()
2826
with response_message:
2927
ui.html(response, sanitize=Sanitizer().sanitize)
3028
ui.run_javascript('window.scrollTo(0, document.body.scrollHeight)')
3129
message_container.remove(spinner)
3230

33-
ui.add_css(r'a:link, a:visited {color: inherit !important; text-decoration: none; font-weight: 500}')
34-
35-
# the queries below are used to expand the contend down to the footer (content can then use flex-grow to expand)
36-
ui.query('.q-page').classes('flex')
37-
ui.query('.nicegui-content').classes('w-full')
38-
39-
with ui.tabs().classes('w-full') as tabs:
40-
chat_tab = ui.tab('Chat')
41-
logs_tab = ui.tab('Logs')
42-
with ui.tab_panels(tabs, value=chat_tab).classes('w-full max-w-2xl mx-auto flex-grow items-stretch'):
43-
message_container = ui.tab_panel(chat_tab).classes('items-stretch')
44-
with ui.tab_panel(logs_tab):
45-
log = ui.log().classes('w-full h-full')
31+
message_container = ui.column().classes('w-full max-w-2xl mx-auto flex-grow items-stretch')
4632

4733
with ui.footer().classes('bg-white'), ui.column().classes('w-full max-w-3xl mx-auto my-6'):
4834
with ui.row().classes('w-full no-wrap items-center'):
@@ -51,7 +37,8 @@ async def send() -> None:
5137
text = ui.input(placeholder=placeholder).props('rounded outlined input-class=mx-3') \
5238
.classes('w-full self-center').on('keydown.enter', send)
5339
ui.markdown('simple chat app built with [NiceGUI](https://nicegui.io)') \
54-
.classes('text-xs self-end mr-8 m-[-1em] text-primary')
40+
.classes('text-xs self-end mr-8 m-[-1em] text-primary') \
41+
.classes('[&_a]:text-inherit [&_a]:no-underline [&_a]:font-medium')
5542

5643

57-
ui.run(title='Chat with GPT-3 (example)')
44+
ui.run(root, title='Chat with GPT-4o-mini')
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
html-sanitizer>=2.6.0
2-
langchain>=0.2
3-
langchain-community
4-
langchain_openai
2+
langchain_openai>=0.3
53
nicegui>=3.0

0 commit comments

Comments
 (0)