Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### Q&A Chatbot ###
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from langchain_openai import ChatOpenAI
|
| 6 |
+
from langchain.prompts.chat import ChatPromptTemplate
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
## Load Anthropic model and get response
|
| 10 |
+
|
| 11 |
+
def get_openai_response(question):
|
| 12 |
+
system_template = "You are a helpful coder assistant. When the user asks a question, your task is to write simple Python codes."
|
| 13 |
+
user_template = "{question}"
|
| 14 |
+
chat_prompt = ChatPromptTemplate.from_messages([
|
| 15 |
+
("system", system_template),
|
| 16 |
+
("human", user_template)
|
| 17 |
+
])
|
| 18 |
+
|
| 19 |
+
chat_llm = ChatOpenAI(temperature=0.5)
|
| 20 |
+
|
| 21 |
+
chain = chat_prompt | chat_llm
|
| 22 |
+
|
| 23 |
+
return chain.invoke(question).content
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
## Initialize Streamlit app
|
| 27 |
+
|
| 28 |
+
st.set_page_config(page_title="Simple Langchain App")
|
| 29 |
+
st.header("Coder Q&A Demo")
|
| 30 |
+
|
| 31 |
+
input = st.text_input("Input: ", key=input)
|
| 32 |
+
response = get_openai_response(input)
|
| 33 |
+
|
| 34 |
+
submit = st.button("Ask your question...")
|
| 35 |
+
if submit:
|
| 36 |
+
st.subheader("The response is:")
|
| 37 |
+
st.write(response)
|