Spaces:
Runtime error
Runtime error
J.P. Pérez Tejada commited on
Commit ·
eb0ed9e
1
Parent(s): 046c24f
Update agent
Browse files- GnosisPages.py +3 -4
- gnosis/agent.py +3 -2
- gnosis/builder.py +17 -4
GnosisPages.py
CHANGED
|
@@ -3,16 +3,15 @@
|
|
| 3 |
import os
|
| 4 |
import streamlit as st
|
| 5 |
import openai
|
| 6 |
-
from dotenv import load_dotenv
|
| 7 |
from gnosis.chroma_client import ChromaDB
|
| 8 |
import gnosis.gui_messages as gm
|
| 9 |
from gnosis import settings
|
| 10 |
from gnosis.components.sidebar import sidebar
|
| 11 |
from gnosis.components.main import main
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
load_dotenv()
|
| 15 |
-
|
| 16 |
|
| 17 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 18 |
|
|
|
|
| 3 |
import os
|
| 4 |
import streamlit as st
|
| 5 |
import openai
|
|
|
|
| 6 |
from gnosis.chroma_client import ChromaDB
|
| 7 |
import gnosis.gui_messages as gm
|
| 8 |
from gnosis import settings
|
| 9 |
from gnosis.components.sidebar import sidebar
|
| 10 |
from gnosis.components.main import main
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
from dotenv import load_dotenv
|
| 13 |
|
| 14 |
+
load_dotenv(Path(__file__).parent / ".env")
|
|
|
|
|
|
|
| 15 |
|
| 16 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 17 |
|
gnosis/agent.py
CHANGED
|
@@ -14,7 +14,7 @@ class PDFExplainer:
|
|
| 14 |
self.tools = [
|
| 15 |
Tool.from_function(
|
| 16 |
func=search.run,
|
| 17 |
-
name="
|
| 18 |
description="Useful when you need more context for answering a question.",
|
| 19 |
handle_parsing_errors=True,
|
| 20 |
)
|
|
@@ -26,7 +26,8 @@ class PDFExplainer:
|
|
| 26 |
self.agent = initialize_agent(
|
| 27 |
self.tools,
|
| 28 |
llm,
|
| 29 |
-
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
|
|
|
| 30 |
verbose=False,
|
| 31 |
handle_parsing_errors=True,
|
| 32 |
)
|
|
|
|
| 14 |
self.tools = [
|
| 15 |
Tool.from_function(
|
| 16 |
func=search.run,
|
| 17 |
+
name="Search_on_ChromaDB",
|
| 18 |
description="Useful when you need more context for answering a question.",
|
| 19 |
handle_parsing_errors=True,
|
| 20 |
)
|
|
|
|
| 26 |
self.agent = initialize_agent(
|
| 27 |
self.tools,
|
| 28 |
llm,
|
| 29 |
+
#agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 30 |
+
agent=AgentType.OPENAI_FUNCTIONS, # ← uses OpenAI's native function calling
|
| 31 |
verbose=False,
|
| 32 |
handle_parsing_errors=True,
|
| 33 |
)
|
gnosis/builder.py
CHANGED
|
@@ -1,24 +1,37 @@
|
|
| 1 |
"""Module for building the Langchain Agent"""
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from langchain_openai import ChatOpenAI
|
| 4 |
from gnosis.agent import PDFExplainer
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
def build(key, client):
|
| 8 |
"""An Agent builder"""
|
| 9 |
# Build Agent
|
| 10 |
try:
|
|
|
|
| 11 |
llm = ChatOpenAI(
|
| 12 |
temperature=st.session_state.temperature,
|
| 13 |
model="gpt-4o-mini",
|
| 14 |
-
api_key=key
|
| 15 |
-
|
| 16 |
agent = PDFExplainer(
|
| 17 |
llm,
|
| 18 |
client,
|
| 19 |
extra_tools=st.session_state.wk_button,
|
| 20 |
).agent
|
| 21 |
-
except Exception: # pylint: disable=broad-exception-caught
|
| 22 |
-
st.warning("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
return agent
|
|
|
|
| 1 |
"""Module for building the Langchain Agent"""
|
| 2 |
+
import os
|
| 3 |
import streamlit as st
|
| 4 |
from langchain_openai import ChatOpenAI
|
| 5 |
from gnosis.agent import PDFExplainer
|
| 6 |
+
from openai import AuthenticationError, APIConnectionError, BadRequestError
|
| 7 |
|
| 8 |
|
| 9 |
def build(key, client):
|
| 10 |
"""An Agent builder"""
|
| 11 |
# Build Agent
|
| 12 |
try:
|
| 13 |
+
key = os.environ.get("OPENAI_API_KEY") or key
|
| 14 |
llm = ChatOpenAI(
|
| 15 |
temperature=st.session_state.temperature,
|
| 16 |
model="gpt-4o-mini",
|
| 17 |
+
api_key=key
|
| 18 |
+
)
|
| 19 |
agent = PDFExplainer(
|
| 20 |
llm,
|
| 21 |
client,
|
| 22 |
extra_tools=st.session_state.wk_button,
|
| 23 |
).agent
|
| 24 |
+
except Exception as e: # pylint: disable=broad-exception-caught
|
| 25 |
+
st.warning(f"Error building agent: {e}")
|
| 26 |
+
except AuthenticationError:
|
| 27 |
+
st.warning("Invalid OpenAI API Key. Check your credentials.")
|
| 28 |
+
except APIConnectionError:
|
| 29 |
+
st.warning("Could not connect to OpenAI. Check your network connection.")
|
| 30 |
+
except BadRequestError as e:
|
| 31 |
+
st.warning(f"Bad request: {e}")
|
| 32 |
+
except ValueError as e:
|
| 33 |
+
st.warning(f"Configuration error: {e}")
|
| 34 |
+
except Exception as e: # pylint: disable=broad-exception-caught
|
| 35 |
+
st.warning(f"Unexpected error building agent: {e}")
|
| 36 |
|
| 37 |
return agent
|