jbellsolutions commited on
Commit
054fe2e
·
1 Parent(s): 82d6ddc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -64
app.py CHANGED
@@ -1,76 +1,42 @@
 
1
  import streamlit as st
2
 
3
- from langchain.agents import AgentExecutor
4
- from langchain.llms import OpenAI
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
- # Initialize OpenAI LLM
10
- llm = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) # Access API key from environment variable
 
11
 
12
- # Define prompts
13
- chat_prompt = """
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
- # Initialize agent
21
- agent = ChatAgent(
22
- llm=llm,
23
- prompt=chat_prompt,
24
- memory=ConversationBufferMemory(),
25
- )
26
 
27
- # Initialize AgentExecutor
28
- agent_executor = AgentExecutor(agent=agent)
 
 
 
29
 
30
- # Streamlit app
31
- st.title("Langchain Chat & Document Analysis")
32
 
33
- # Upload document
34
- uploaded_file = st.file_uploader("Upload a document (txt, pdf, doc, docx):")
35
 
36
- # Input field for user query
37
- user_query = st.text_area("Ask me anything:")
38
 
39
- # Choose document or chat mode
40
- is_doc_mode = uploaded_file is not None
41
 
42
- # Execute agent based on mode
43
- if is_doc_mode:
44
- # Load document text
45
- doc_text = load_text_file(uploaded_file)
46
 
47
- # Update chat prompt with document information
48
- agent.update_prompt(doc_prompt, {"doc_text": doc_text})
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)