stream chat
from langchain_community.chat_models.tongyi import ChatTongyi
import streamlit as st
from openai import OpenAI
st.title("ChatGPT-like clone")
# Set OpenAI API key from Streamlit secrets
# client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
client = ChatTongyi(model='qwen2-72b-instruct', top_p=0.8, streaming=True)
# Set a default model
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-3.5-turbo"
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Accept user input
if prompt := st.chat_input("Please type a message..."):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Display assistant response in chat message container
with st.chat_message("assistant"):
stream = client.stream(
input=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
streaming=True,
)
response = st.write_stream(stream)
st.session_state.messages.append({"role": "assistant", "content": response})
onechat
import streamlit as st
from langchain_community.chat_models.tongyi import ChatTongyi
from langchain.prompts import ChatPromptTemplate
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
from langchain.memory import ConversationBufferMemory
from langchain.chains import SequentialChain, ConversationChain
from dotenv import load_dotenv
import json
load_dotenv()
st.title('Router Mode')
llm = ChatTongyi(model='qwen2-72b-instruct', top_p=0.8, streaming=True)
def generate_response(input_text):
# resp = llm.invoke(input_text).content
chunks = ""
obj = st.empty()
for chunk in llm.stream(input_text):
chunks += chunk.content
print(chunk.content, end='', flush=True)
obj.info(chunks)
with st.form('my_form'):
text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?')
submitted = st.form_submit_button('Submit')
if submitted:
generate_response(text)