Spaces:
Build error
Build error
Commit ·
054fe2e
1
Parent(s): 82d6ddc
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,42 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
from langchain.memory import ConversationBufferMemory
|
| 6 |
-
from langchain.agents.agent_types import ChatAgent
|
| 7 |
-
from langchain.data_ops import load_text_file, summarize, question_answering, extract_keywords, search
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
Hi there! I'm your Langchain assistant. Ask me anything about Langchain or upload a document for me to analyze.
|
| 15 |
-
"""
|
| 16 |
-
doc_prompt = """
|
| 17 |
-
What would you like me to do with this document? (e.g., summarize, answer questions, extract keywords, search documentation for relevant information)
|
| 18 |
-
"""
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
prompt=chat_prompt,
|
| 24 |
-
memory=ConversationBufferMemory(),
|
| 25 |
-
)
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
if
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
# Get user instructions for document processing
|
| 51 |
-
doc_action = st.text_input("Instructions for document processing:")
|
| 52 |
-
|
| 53 |
-
# Analyze document based on user instructions
|
| 54 |
-
if "summarize" in doc_action.lower():
|
| 55 |
-
summary = summarize(doc_text)
|
| 56 |
-
st.write(f"Document summary: {summary}")
|
| 57 |
-
elif "answer question" in doc_action.lower():
|
| 58 |
-
answer = question_answering(doc_text, doc_action)
|
| 59 |
-
st.write(f"Answer to your question: {answer}")
|
| 60 |
-
elif "extract keywords" in doc_action.lower():
|
| 61 |
-
keywords = extract_keywords(doc_text)
|
| 62 |
-
st.write(f"Keywords extracted: {keywords}")
|
| 63 |
-
elif "search documentation" in doc_action.lower():
|
| 64 |
-
search_results = search(doc_action, sources=["documentation"])
|
| 65 |
-
st.write(f"Documentation search results: {search_results}")
|
| 66 |
-
else:
|
| 67 |
-
st.write("Please provide specific instructions for document processing.")
|
| 68 |
-
|
| 69 |
-
# Update chat prompt with document removed
|
| 70 |
-
agent.update_prompt(chat_prompt)
|
| 71 |
-
else:
|
| 72 |
-
# Execute agent with user query
|
| 73 |
-
response = agent_executor.ainvoke({"input": user_query})
|
| 74 |
-
|
| 75 |
-
# Display response
|
| 76 |
-
st.write(response["output"])
|
|
|
|
| 1 |
+
import langchain
|
| 2 |
import streamlit as st
|
| 3 |
|
| 4 |
+
# Create a LangChain client
|
| 5 |
+
client = langchain.Client()
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Define the function that will generate code for a given prompt
|
| 8 |
+
def generate_code(prompt):
|
| 9 |
+
"""Generates code for a given prompt.
|
| 10 |
|
| 11 |
+
Args:
|
| 12 |
+
prompt: The prompt to generate code for.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
Returns:
|
| 15 |
+
The generated code.
|
| 16 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Create a request object
|
| 19 |
+
request = langchain.GenerateCodeRequest(
|
| 20 |
+
prompt=prompt,
|
| 21 |
+
language="python",
|
| 22 |
+
num_tokens=100)
|
| 23 |
|
| 24 |
+
# Send the request to the LangChain API
|
| 25 |
+
response = client.generate_code(request)
|
| 26 |
|
| 27 |
+
# Return the generated code
|
| 28 |
+
return response.code
|
| 29 |
|
| 30 |
+
# Create a Streamlit app
|
| 31 |
+
st.title("LangChain Code Specialist")
|
| 32 |
|
| 33 |
+
# Create a text input field for the user to enter a prompt
|
| 34 |
+
prompt = st.text_input("Enter a prompt:")
|
| 35 |
|
| 36 |
+
# Create a button for the user to click to generate code
|
| 37 |
+
if st.button("Generate Code"):
|
| 38 |
+
# Generate code for the given prompt
|
| 39 |
+
code = generate_code(prompt)
|
| 40 |
|
| 41 |
+
# Display the generated code
|
| 42 |
+
st.code(code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|