Skip to content

Commit

Permalink
Merge pull request #123 from holoviz-topics/langchain_lcel
Browse files Browse the repository at this point in the history
Add example for Langchain LCEL
  • Loading branch information
ahuang11 authored Jan 19, 2024
2 parents f39eddd + a045834 commit d75d94a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
Binary file added docs/assets/thumbnails/langchain_lcel.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/videos/langchain_lcel.mp4
Binary file not shown.
34 changes: 34 additions & 0 deletions docs/examples/langchain/langchain_lcel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Demonstrates how to use the `ChatInterface` to create a chatbot using
[LangChain Expression Language](https://python.langchain.com/docs/expression_language/) (LCEL).
"""

import panel as pn
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_openai import ChatOpenAI

pn.extension()


async def callback(contents, user, instance):
message = ""
async for token in chain.astream(contents):
message += token
yield message


prompt = ChatPromptTemplate.from_template("Tell me a top-notch joke about {topic}")
model = ChatOpenAI(model="gpt-3.5-turbo")
output_parser = StrOutputParser()
chain = {"topic": RunnablePassthrough()} | prompt | model | output_parser

chat_interface = pn.chat.ChatInterface(
pn.chat.ChatMessage(
"Offer a topic and ChatGPT will respond with a joke!", user="System"
),
callback=callback,
callback_user="ChatGPT",
)
chat_interface.servable()
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ dependencies = [
"tabulate",
"tiktoken",
"mistralai",
"langchain-openai",
]

[project.urls]
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ def langchain_pdf_assistant(page: Page):
page.wait_for_timeout(10000)


def langchain_lcel(page: Page):
chat = ChatInterface(page)
chat.send("Python")
page.wait_for_timeout(5000)


def mistral_and_llama(page: Page):
chat = ChatInterface(page)
chat.send("What do you think about HoloViz in a single sentence?")
Expand Down

0 comments on commit d75d94a

Please sign in to comment.