Add a string at the end of response using Langchain #25565
Unanswered
sherlock-tez
asked this question in
Q&A
Replies: 1 comment
-
To add a string like "Thank you for your question" at the end of every response when streaming using LangChain, you can modify the code to append this string after the streaming loop. Here is how you can do it for both synchronous and asynchronous streaming: Synchronous Streamingchunks = []
for chunk in model.stream("what color is the sky?"):
chunks.append(chunk)
print(chunk.content, end="|", flush=True)
print("Thank you for your question", end="|", flush=True) Asynchronous Streamingchunks = []
async for chunk in model.astream("what color is the sky?"):
chunks.append(chunk)
print(chunk.content, end="|", flush=True)
print("Thank you for your question", end="|", flush=True) Using LangChain with TextGenIf you are using LangChain with the TextGen integration, you can modify the streaming example as follows: model_url = "ws://localhost:5005"
from langchain.chains import LLMChain
from langchain.globals import set_debug
from langchain_community.llms import TextGen
from langchain_core.callbacks import StreamingStdOutCallbackHandler
from langchain_core.prompts import PromptTemplate
set_debug(True)
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)
llm = TextGen(
model_url=model_url, streaming=True, callbacks=[StreamingStdOutCallbackHandler()]
)
llm_chain = LLMChain(prompt=prompt, llm=llm)
question = "What NFL team won the Super Bowl in the year Justin Bieber was born?"
llm_chain.run(question)
# Custom streaming loop
for chunk in llm.stream(question):
print(chunk, end="", flush=True)
print("Thank you for your question", end="", flush=True) Using LangChain with PremAIIf you are using LangChain with the PremAI integration, you can modify the streaming example as follows: import sys
from langchain_community.chat_models import ChatPremAI
from langchain_core.messages import HumanMessage
# Setup PremAI client
chat = ChatPremAI(project_id=1234, model_name="gpt-4o")
# Custom streaming loop
for chunk in chat.stream("hello how are you"):
sys.stdout.write(chunk.content)
sys.stdout.flush()
sys.stdout.write("Thank you for your question")
sys.stdout.flush() In all cases, the string "Thank you for your question" is printed at the end of the response [1][2][3]. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
Hi the task is very simple but I can not find the solution. This is my code. I just want to add for example "Thank you for your question" at the end of every response in when streaming. How to do it? Thank you.
System Info
MacOS 14.4
Python 3.11
langchain-openai = "^0.1.14"
azure-search-documents = "=11.6.0b4"
langchain-community = "=0.2.6"
langchain-core = "=0.2.11"
Beta Was this translation helpful? Give feedback.
All reactions