-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/holoviz-topics/panel-chat-e…
…xamples into enhancement/news-summarizer
- Loading branch information
Showing
12 changed files
with
120 additions
and
42 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
56 changes: 56 additions & 0 deletions
56
docs/examples/langchain/langchain_streaming_lcel_with_memory.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
""" | ||
Demonstrates how to use the `ChatInterface` to create a chatbot using | ||
[LangChain Expression Language](https://python.langchain.com/docs/expression_language/) (LCEL) | ||
with streaming and memory. | ||
""" | ||
|
||
from operator import itemgetter | ||
|
||
import panel as pn | ||
from langchain.memory import ConversationSummaryBufferMemory | ||
from langchain_core.output_parsers import StrOutputParser | ||
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder | ||
from langchain_core.runnables import RunnableLambda, RunnablePassthrough | ||
from langchain_openai import ChatOpenAI | ||
|
||
pn.extension() | ||
|
||
SYSTEM_PROMPT = "Try to be a silly comedian." | ||
|
||
|
||
async def callback(contents, user, instance): | ||
message = "" | ||
inputs = {"input": contents} | ||
async for token in chain.astream(inputs): | ||
message += token | ||
yield message | ||
memory.save_context(inputs, {"output": message}) | ||
|
||
|
||
model = ChatOpenAI(model="gpt-3.5-turbo") | ||
memory = ConversationSummaryBufferMemory(return_messages=True, llm=model) | ||
prompt = ChatPromptTemplate.from_messages( | ||
[ | ||
("system", SYSTEM_PROMPT), | ||
MessagesPlaceholder(variable_name="history"), | ||
("human", "{input}"), | ||
] | ||
) | ||
output_parser = StrOutputParser() | ||
chain = ( | ||
RunnablePassthrough.assign( | ||
history=RunnableLambda(memory.load_memory_variables) | itemgetter("history") | ||
) | ||
| prompt | ||
| model | ||
| output_parser | ||
) | ||
|
||
chat_interface = pn.chat.ChatInterface( | ||
pn.chat.ChatMessage( | ||
"Offer a topic and ChatGPT will try to be funny!", user="System" | ||
), | ||
callback=callback, | ||
callback_user="ChatGPT", | ||
) | ||
chat_interface.servable() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,7 @@ dependencies = [ | |
"tabulate", | ||
"tiktoken", | ||
"mistralai", | ||
"langchain-openai", | ||
] | ||
|
||
[project.urls] | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters