Spaces:
Sleeping
Sleeping
Commit
·
b32dd1a
1
Parent(s):
6e1855f
streamlit push
Browse files- app.py +90 -0
- requirements.txt +9 -0
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain_openai import ChatOpenAI
|
| 3 |
+
from langchain_core.messages import HumanMessage, AIMessage, trim_messages
|
| 4 |
+
from langgraph.checkpoint.memory import MemorySaver
|
| 5 |
+
from langgraph.graph import START, MessagesState, StateGraph
|
| 6 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def get_model():
|
| 12 |
+
model = ChatOpenAI(model="gpt-4o", temperature=0, base_url="https://models.inference.ai.azure.com")
|
| 13 |
+
return model
|
| 14 |
+
|
| 15 |
+
model = get_model()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
system_prompt = """You are an intelligent and versatile AI assistant, capable of engaging in natural, helpful, and coherent conversations. Your primary role is to assist users with a wide range of topics, including answering questions, providing recommendations, solving problems, generating creative content, and offering technical guidance.
|
| 19 |
+
|
| 20 |
+
Key Guidelines:
|
| 21 |
+
|
| 22 |
+
1. Clarity and Precision: Provide clear, concise, and accurate responses. Tailor your tone and style to match the user’s needs and preferences.
|
| 23 |
+
|
| 24 |
+
2. Helpfulness: Strive to be as useful as possible. Clarify ambiguous queries and ask for more details when needed.
|
| 25 |
+
|
| 26 |
+
3. Adaptability: Adjust your responses based on the context and complexity of the user's request, from casual to professional interactions.
|
| 27 |
+
|
| 28 |
+
4. Ethical and Safe: Ensure your responses are ethical, unbiased, and do not promote harm, misinformation, or illegal activities.
|
| 29 |
+
|
| 30 |
+
5. Context Awareness: Leverage the context of the conversation to provide relevant and coherent replies, maintaining continuity throughout.
|
| 31 |
+
|
| 32 |
+
6. Creative Problem-Solving: When asked for creative or technical solutions, provide innovative, practical, and actionable ideas.
|
| 33 |
+
|
| 34 |
+
7. Limitations: Be transparent about your capabilities and limitations. If you cannot answer a question or perform a task, communicate this clearly and, when possible, suggest alternative resources.
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
prompt_template = ChatPromptTemplate.from_messages(
|
| 39 |
+
[
|
| 40 |
+
(
|
| 41 |
+
"system",
|
| 42 |
+
system_prompt,
|
| 43 |
+
),
|
| 44 |
+
MessagesPlaceholder(variable_name="messages"),
|
| 45 |
+
]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
trimmer = trim_messages(
|
| 49 |
+
max_tokens=65,
|
| 50 |
+
strategy="last",
|
| 51 |
+
token_counter=model,
|
| 52 |
+
include_system=True,
|
| 53 |
+
allow_partial=False,
|
| 54 |
+
start_on="human",
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def call_model(state: MessagesState):
|
| 59 |
+
trimmed_messages = trimmer.invoke(state["messages"])
|
| 60 |
+
prompt = prompt_template.invoke({"messages" : trimmed_messages})
|
| 61 |
+
response = model.invoke(prompt)
|
| 62 |
+
return {"messages": response}
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
@st.cache_resource
|
| 66 |
+
def get_app(state=MessagesState):
|
| 67 |
+
|
| 68 |
+
workflow = StateGraph(state_schema=MessagesState)
|
| 69 |
+
workflow.add_edge(START, "model")
|
| 70 |
+
workflow.add_node("model", call_model)
|
| 71 |
+
|
| 72 |
+
memory = MemorySaver()
|
| 73 |
+
app = workflow.compile(checkpointer=memory)
|
| 74 |
+
return app
|
| 75 |
+
|
| 76 |
+
config = {"configurable": {"thread_id": "111"}}
|
| 77 |
+
app = get_app()
|
| 78 |
+
|
| 79 |
+
if query:=st.chat_input("Ask anything"):
|
| 80 |
+
msg= [HumanMessage(query)]
|
| 81 |
+
def gen():
|
| 82 |
+
for chunk, metadata in app.stream({"messages": msg}, config=config, stream_mode="messages"):
|
| 83 |
+
if isinstance(chunk, AIMessage):
|
| 84 |
+
yield chunk.content
|
| 85 |
+
st.write_stream(gen)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain_community
|
| 2 |
+
langchain-openai
|
| 3 |
+
langchain
|
| 4 |
+
langgraph
|
| 5 |
+
langchainhub
|
| 6 |
+
streamlit
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|