EmbeddedAndrew commited on
Commit
5f5253e
Β·
1 Parent(s): cb1c674

LC streaming

Browse files
Files changed (2) hide show
  1. LC_non-streaming.py +24 -0
  2. app.py +30 -14
LC_non-streaming.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # working
2
+ # Non-streaming chat.
3
+ # langchain llm, NOT CHAT_LLM
4
+
5
+ import streamlit as st
6
+ from langchain.llms import OpenAI
7
+ import os
8
+
9
+
10
+ st.title('πŸ¦œπŸ”— Dev App')
11
+
12
+ openai_api_key = os.environ["OPENAI_API_KEY"] #st.sidebar.text_input('OpenAI API Key')
13
+
14
+ def generate_response(input_text):
15
+ llm = OpenAI(temperature=0.0, openai_api_key=openai_api_key)
16
+ st.info(llm(input_text))
17
+
18
+ with st.form('my_form'):
19
+ text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?')
20
+ submitted = st.form_submit_button('Submit')
21
+ if not openai_api_key.startswith('sk-'):
22
+ st.warning('Please enter your OpenAI API key!', icon='⚠')
23
+ if submitted and openai_api_key.startswith('sk-'):
24
+ generate_response(text)
app.py CHANGED
@@ -1,20 +1,36 @@
 
 
 
 
 
 
1
  import streamlit as st
2
- from langchain.llms import OpenAI
3
- import os
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- st.title('πŸ¦œπŸ”— Dev App')
 
7
 
8
- openai_api_key = os.environ["OPENAI_API_KEY"] #st.sidebar.text_input('OpenAI API Key')
 
 
 
9
 
10
- def generate_response(input_text):
11
- llm = OpenAI(temperature=0.0, openai_api_key=openai_api_key)
12
- st.info(llm(input_text))
13
 
14
- with st.form('my_form'):
15
- text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?')
16
- submitted = st.form_submit_button('Submit')
17
- if not openai_api_key.startswith('sk-'):
18
- st.warning('Please enter your OpenAI API key!', icon='⚠')
19
- if submitted and openai_api_key.startswith('sk-'):
20
- generate_response(text)
 
1
+ # LC_Streaming.py
2
+
3
+
4
+ from langchain.callbacks.base import BaseCallbackHandler
5
+ from langchain.chat_models import ChatOpenAI
6
+ from langchain.schema import HumanMessage
7
  import streamlit as st
 
 
8
 
9
+ class StreamHandler(BaseCallbackHandler):
10
+ def __init__(self, container, initial_text="", display_method='markdown'):
11
+ self.container = container
12
+ self.text = initial_text
13
+ self.display_method = display_method
14
+
15
+ def on_llm_new_token(self, token: str, **kwargs) -> None:
16
+ self.text += token + "/"
17
+ display_function = getattr(self.container, self.display_method, None)
18
+ if display_function is not None:
19
+ display_function(self.text)
20
+ else:
21
+ raise ValueError(f"Invalid display_method: {self.display_method}")
22
 
23
+ query = st.text_input("input your query", value="Tell me a joke")
24
+ ask_button = st.button("ask")
25
 
26
+ st.markdown("### streaming box")
27
+ chat_box = st.empty()
28
+ stream_handler = StreamHandler(chat_box, display_method='write')
29
+ chat = ChatOpenAI(max_tokens=25, streaming=True, callbacks=[stream_handler])
30
 
31
+ st.markdown("### together box")
 
 
32
 
33
+ if query and ask_button:
34
+ response = chat([HumanMessage(content=query)])
35
+ llm_response = response.content
36
+ st.markdown(llm_response)