Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import json
|
| 3 |
-
|
| 4 |
import time
|
| 5 |
|
| 6 |
# Initialize Streamlit page configuration
|
|
@@ -11,18 +11,15 @@ st.set_page_config(
|
|
| 11 |
)
|
| 12 |
|
| 13 |
# Secret management
|
| 14 |
-
def
|
| 15 |
# In production, use Streamlit secrets
|
| 16 |
-
return st.secrets["
|
| 17 |
|
| 18 |
-
# Initialize
|
| 19 |
@st.cache_resource
|
| 20 |
-
def
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
token=get_hf_api_token()
|
| 24 |
-
)
|
| 25 |
-
return client
|
| 26 |
|
| 27 |
# Load and cache knowledge base
|
| 28 |
@st.cache_data
|
|
@@ -74,9 +71,9 @@ def get_context(query: str, knowledge_base: dict) -> str:
|
|
| 74 |
|
| 75 |
return "\n".join(contexts)
|
| 76 |
|
| 77 |
-
def
|
| 78 |
-
"""Generate prompt for
|
| 79 |
-
return f"""
|
| 80 |
|
| 81 |
Context:
|
| 82 |
{context}
|
|
@@ -89,36 +86,35 @@ Instructions:
|
|
| 89 |
- Be specific about technical details and achievements
|
| 90 |
- Keep responses concise but informative
|
| 91 |
- Focus on relevant experience and skills
|
| 92 |
-
- Maintain a professional tone
|
| 93 |
-
|
| 94 |
-
Your response: [/INST]"""
|
| 95 |
|
| 96 |
def get_chat_response(query: str, knowledge_base: dict) -> str:
|
| 97 |
-
"""Get response from
|
| 98 |
try:
|
| 99 |
# Get context
|
| 100 |
context = get_context(query, knowledge_base)
|
| 101 |
|
| 102 |
# Generate prompt
|
| 103 |
-
prompt =
|
| 104 |
|
| 105 |
-
# Get client
|
| 106 |
-
client =
|
| 107 |
|
| 108 |
# Generate response
|
| 109 |
-
response = client.
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
| 115 |
)
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
|
| 119 |
-
response = response.split("[/INST]")[-1].strip()
|
| 120 |
|
| 121 |
-
return
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
st.error(f"Error generating response: {str(e)}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import json
|
| 3 |
+
import openai
|
| 4 |
import time
|
| 5 |
|
| 6 |
# Initialize Streamlit page configuration
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
# Secret management
|
| 14 |
+
def get_openai_api_token():
|
| 15 |
# In production, use Streamlit secrets
|
| 16 |
+
return st.secrets["OPENAI_API_KEY"]
|
| 17 |
|
| 18 |
+
# Initialize OpenAI client
|
| 19 |
@st.cache_resource
|
| 20 |
+
def get_openai_client():
|
| 21 |
+
openai.api_key = get_openai_api_token()
|
| 22 |
+
return openai
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Load and cache knowledge base
|
| 25 |
@st.cache_data
|
|
|
|
| 71 |
|
| 72 |
return "\n".join(contexts)
|
| 73 |
|
| 74 |
+
def generate_openai_prompt(query: str, context: str) -> str:
|
| 75 |
+
"""Generate prompt for OpenAI model"""
|
| 76 |
+
return f"""You are Manyue's AI assistant. Use this context to answer questions about Manyue:
|
| 77 |
|
| 78 |
Context:
|
| 79 |
{context}
|
|
|
|
| 86 |
- Be specific about technical details and achievements
|
| 87 |
- Keep responses concise but informative
|
| 88 |
- Focus on relevant experience and skills
|
| 89 |
+
- Maintain a professional tone"""
|
|
|
|
|
|
|
| 90 |
|
| 91 |
def get_chat_response(query: str, knowledge_base: dict) -> str:
|
| 92 |
+
"""Get response from OpenAI API"""
|
| 93 |
try:
|
| 94 |
# Get context
|
| 95 |
context = get_context(query, knowledge_base)
|
| 96 |
|
| 97 |
# Generate prompt
|
| 98 |
+
prompt = generate_openai_prompt(query, context)
|
| 99 |
|
| 100 |
+
# Get OpenAI client
|
| 101 |
+
client = get_openai_client()
|
| 102 |
|
| 103 |
# Generate response
|
| 104 |
+
response = client.ChatCompletion.create(
|
| 105 |
+
model="gpt-3.5-turbo", # You can change to gpt-4 if preferred
|
| 106 |
+
messages=[
|
| 107 |
+
{"role": "system", "content": prompt},
|
| 108 |
+
{"role": "user", "content": query}
|
| 109 |
+
],
|
| 110 |
+
max_tokens=200,
|
| 111 |
+
temperature=0.7
|
| 112 |
)
|
| 113 |
|
| 114 |
+
# Extract and clean response
|
| 115 |
+
response_text = response.choices[0].message.content.strip()
|
|
|
|
| 116 |
|
| 117 |
+
return response_text
|
| 118 |
|
| 119 |
except Exception as e:
|
| 120 |
st.error(f"Error generating response: {str(e)}")
|