oluinioluwa814 commited on
Commit
963bcb7
·
verified ·
1 Parent(s): e543d01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -53
app.py CHANGED
@@ -1,99 +1,128 @@
1
- from dotenv import load_dotenv
2
  import os
 
 
 
3
  from langchain_openai import ChatOpenAI, OpenAIEmbeddings
4
  from langchain_astradb import AstraDBVectorStore
5
- from langchain.agents import initialize_agent, AgentType
6
- from langchain.tools.retriever import create_retriever_tool
7
  from langchain.agents import AgentExecutor
8
- from github import fetch_github_issues
9
- import gradio as gr
 
 
10
  from langchain import hub
 
 
11
  from note import note_tool
12
 
 
 
 
13
  load_dotenv()
14
 
15
- def connet_to_vstore():
 
 
 
16
  embeddings = OpenAIEmbeddings()
17
- desire_namespace = os.getenv("ASTRA_DB_KEYSPACE")
18
- ASTRA_DB_APPLICATION_TOKEN = os.getenv("ASTRA_DB_APPLICATION_TOKEN")
19
- ASTRA_DB_API_ENDPOINT = os.getenv("ASTRA_DB_API_ENDPOINT")
20
- if desire_namespace:
21
- ASTRA_DB_KEYSPACE = desire_namespace
22
- else:
23
- ASTRA_DB_KEYSPACE = None
24
-
25
- vstore = AstraDBVectorStore(
26
  embedding=embeddings,
27
  collection_name="github",
28
- namespace=ASTRA_DB_KEYSPACE,
29
- api_endpoint=ASTRA_DB_API_ENDPOINT,
30
- token=ASTRA_DB_APPLICATION_TOKEN,
31
  )
32
- return vstore
33
 
34
- vstore = connet_to_vstore()
35
- add_to_vectorstore = input("Do you want to update the issue? (yes/no): ").lower() in ["yes", "y"]
36
- if add_to_vectorstore:
 
 
 
 
 
 
 
37
  owner = "Ini-design"
38
  repo = "register"
39
  issues = fetch_github_issues(owner, repo)
 
40
  try:
41
  vstore.delete_collection()
42
- except:
43
  pass
44
-
45
- vstore = connet_to_vstore()
46
  vstore.add_documents(issues)
47
-
48
- # results = vstore.similarity_search('flash message', k=3)
49
- # for res in results:
50
- # print(f"*{res.page_content} {res.metadata}")
51
-
52
- retriever = vstore.as_retriever(search_kwargs={"k":3})
53
  retriever_tool = create_retriever_tool(
54
  retriever,
55
- "github_search",
56
- "search for information aabout github issues. For any question abour github issue, you must used this tools!"
 
 
 
57
  )
58
 
59
- prompt = hub.pull("hwchase17/openai-functions_agent")
60
- llm = ChatOpenAI()
61
 
62
- tools = [retriever_tool], note_tool
63
- agent = initialize_agent(llm, tools, prompt)
64
- agent_executor = AgentExecutor(agent=AgentType.OPENAI_FUNCTIONS, tools=tools, verbose=True)
 
 
 
 
 
 
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  def answer_question(question):
67
- """Function to process user question and return agent response"""
68
  if not question.strip():
69
  return "Please enter a question."
70
-
71
  try:
72
- result = agent_executor.invoke({"input": question})
73
- return result["output"]
74
  except Exception as e:
75
- return f"Error: {str(e)}"
 
76
 
77
- # Create Gradio Interface
78
  demo = gr.Interface(
79
  fn=answer_question,
80
  inputs=gr.Textbox(
81
  label="Ask about GitHub Issues",
82
  placeholder="Type your question here...",
83
- lines=3
84
- ),
85
- outputs=gr.Textbox(
86
- label="Response",
87
- lines=5
88
  ),
 
89
  title="GitHub Issues AI Agent",
90
- description="Ask questions about GitHub issues using AI-powered semantic search",
91
  examples=[
92
  ["What are the recent issues?"],
93
- ["Tell me about bug reports"],
94
- ["What features are being requested?"]
95
  ],
96
- allow_flagging="never"
97
  )
98
 
99
  if __name__ == "__main__":
 
 
1
  import os
2
+ import gradio as gr
3
+ from dotenv import load_dotenv
4
+
5
  from langchain_openai import ChatOpenAI, OpenAIEmbeddings
6
  from langchain_astradb import AstraDBVectorStore
7
+
 
8
  from langchain.agents import AgentExecutor
9
+ from langchain.agents.openai_functions_agent import (
10
+ create_openai_functions_agent,
11
+ )
12
+ from langchain.tools.retriever import create_retriever_tool
13
  from langchain import hub
14
+
15
+ from github import fetch_github_issues
16
  from note import note_tool
17
 
18
+ # --------------------------------------------------
19
+ # ENV
20
+ # --------------------------------------------------
21
  load_dotenv()
22
 
23
+ # --------------------------------------------------
24
+ # VECTOR STORE
25
+ # --------------------------------------------------
26
+ def connect_to_vstore():
27
  embeddings = OpenAIEmbeddings()
28
+
29
+ return AstraDBVectorStore(
 
 
 
 
 
 
 
30
  embedding=embeddings,
31
  collection_name="github",
32
+ namespace=os.getenv("ASTRA_DB_KEYSPACE"),
33
+ api_endpoint=os.getenv("ASTRA_DB_API_ENDPOINT"),
34
+ token=os.getenv("ASTRA_DB_APPLICATION_TOKEN"),
35
  )
 
36
 
37
+
38
+ vstore = connect_to_vstore()
39
+
40
+ # --------------------------------------------------
41
+ # OPTIONAL: UPDATE VECTOR STORE
42
+ # (disable input() for Spaces)
43
+ # --------------------------------------------------
44
+ UPDATE_VECTORSTORE = True # change to False if not needed
45
+
46
+ if UPDATE_VECTORSTORE:
47
  owner = "Ini-design"
48
  repo = "register"
49
  issues = fetch_github_issues(owner, repo)
50
+
51
  try:
52
  vstore.delete_collection()
53
+ except Exception:
54
  pass
55
+
56
+ vstore = connect_to_vstore()
57
  vstore.add_documents(issues)
58
+
59
+ # --------------------------------------------------
60
+ # RETRIEVER TOOL
61
+ # --------------------------------------------------
62
+ retriever = vstore.as_retriever(search_kwargs={"k": 3})
63
+
64
  retriever_tool = create_retriever_tool(
65
  retriever,
66
+ name="github_search",
67
+ description=(
68
+ "Search for information about GitHub issues. "
69
+ "Use this tool for any GitHub issue-related questions."
70
+ ),
71
  )
72
 
73
+ tools = [retriever_tool, note_tool]
 
74
 
75
+ # --------------------------------------------------
76
+ # AGENT
77
+ # --------------------------------------------------
78
+ prompt = hub.pull("hwchase17/openai-functions-agent")
79
+
80
+ llm = ChatOpenAI(
81
+ model="gpt-3.5-turbo",
82
+ temperature=0,
83
+ )
84
 
85
+ agent = create_openai_functions_agent(
86
+ llm=llm,
87
+ tools=tools,
88
+ prompt=prompt,
89
+ )
90
+
91
+ agent_executor = AgentExecutor(
92
+ agent=agent,
93
+ tools=tools,
94
+ verbose=True,
95
+ )
96
+
97
+ # --------------------------------------------------
98
+ # GRADIO APP
99
+ # --------------------------------------------------
100
  def answer_question(question):
 
101
  if not question.strip():
102
  return "Please enter a question."
103
+
104
  try:
105
+ response = agent_executor.invoke({"input": question})
106
+ return response["output"]
107
  except Exception as e:
108
+ return f"Error: {e}"
109
+
110
 
 
111
  demo = gr.Interface(
112
  fn=answer_question,
113
  inputs=gr.Textbox(
114
  label="Ask about GitHub Issues",
115
  placeholder="Type your question here...",
116
+ lines=3,
 
 
 
 
117
  ),
118
+ outputs=gr.Textbox(label="Response", lines=6),
119
  title="GitHub Issues AI Agent",
120
+ description="Ask questions about GitHub issues using AI-powered semantic search.",
121
  examples=[
122
  ["What are the recent issues?"],
123
+ ["Are there any open bugs?"],
124
+ ["What features are being requested?"],
125
  ],
 
126
  )
127
 
128
  if __name__ == "__main__":