Skip to content

Commit

Permalink
Langchain upgrade (#339)
Browse files Browse the repository at this point in the history
* WIP: langchain upgrade

* debugging WIP

* WIP notebook

* langchain handler and example upgrade

* code cleanup

* black formatting

* remove logger setup

* cleanup

* use logger

* import change

* code cleanup

* add code back

* langchain notebook merge

* black formatting
  • Loading branch information
bboynton97 authored Aug 9, 2024
1 parent c572344 commit a12b59a
Show file tree
Hide file tree
Showing 12 changed files with 592 additions and 199 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ To use the handler, import and set
import os
from langchain.chat_models import ChatOpenAI
from langchain.agents import initialize_agent, AgentType
from agentops.langchain_callback_handler import LangchainCallbackHandler
from agentops.partners.langchain_callback_handler import LangchainCallbackHandler

AGENTOPS_API_KEY = os.environ['AGENTOPS_API_KEY']
handler = LangchainCallbackHandler(api_key=AGENTOPS_API_KEY, tags=['Langchain Example'])
Expand Down
3 changes: 3 additions & 0 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def configure(
)

def initialize(self) -> Union[Session, None]:
if self.is_initialized:
return

self.unsuppress_logs()

if self._config.api_key is None:
Expand Down
50 changes: 33 additions & 17 deletions agentops/partners/langchain_callback_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ def __init__(
endpoint: Optional[str] = None,
max_wait_time: Optional[int] = None,
max_queue_size: Optional[int] = None,
tags: Optional[List[str]] = None,
default_tags: Optional[List[str]] = None,
):

logging_level = os.getenv("AGENTOPS_LOGGING_LEVEL")
log_levels = {
"CRITICAL": logging.CRITICAL,
Expand All @@ -64,12 +63,19 @@ def __init__(
"endpoint": endpoint,
"max_wait_time": max_wait_time,
"max_queue_size": max_queue_size,
"tags": tags,
"default_tags": default_tags,
}

self.ao_client = AOClient(
**{k: v for k, v in client_params.items() if v is not None}, override=False
)
self.ao_client = AOClient()
if self.ao_client.session_count == 0:
self.ao_client.configure(
**{k: v for k, v in client_params.items() if v is not None},
instrument_llm_calls=False,
)

if not self.ao_client.is_initialized:
self.ao_client.initialize()

self.agent_actions: Dict[UUID, List[ActionEvent]] = defaultdict(list)
self.events = Events()

Expand All @@ -93,7 +99,6 @@ def on_llm_start(
}, # TODO: params is inconsistent, in ToolEvent we put it in logs
model=get_model_from_kwargs(kwargs),
prompt=prompts[0],
# tags=tags # TODO
)

@debug_print_function_params
Expand Down Expand Up @@ -156,15 +161,18 @@ def on_chain_start(
metadata: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> Any:
self.events.chain[str(run_id)] = ActionEvent(
params={
**serialized,
**inputs,
**({} if metadata is None else metadata),
**kwargs,
},
action_type="chain",
)
try:
self.events.chain[str(run_id)] = ActionEvent(
params={
**serialized,
**inputs,
**({} if metadata is None else metadata),
**kwargs,
},
action_type="chain",
)
except Exception as e:
logger.warning(e)

@debug_print_function_params
def on_chain_end(
Expand Down Expand Up @@ -240,6 +248,8 @@ def on_tool_end(
details=output,
)
self.ao_client.record(error_event)
else:
self.ao_client.record(tool_event)

@debug_print_function_params
def on_tool_error(
Expand Down Expand Up @@ -357,7 +367,13 @@ def on_retry(

@property
def session_id(self):
return self.ao_client.current_session_id
raise DeprecationWarning(
"session_id is deprecated in favor of current_session_ids"
)

@property
def current_session_ids(self):
return self.ao_client.current_session_ids


class AsyncLangchainCallbackHandler(AsyncCallbackHandler):
Expand Down
1 change: 1 addition & 0 deletions examples/crew/job_posting.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@
"source": [
"from textwrap import dedent\n",
"\n",
"\n",
"class Tasks:\n",
" def research_company_culture_task(self, agent, company_description, company_domain):\n",
" return Task(\n",
Expand Down
10 changes: 7 additions & 3 deletions examples/crew/job_posting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

import os

os.environ["SERPER_API_KEY"] = "..."
os.environ["OPENAI_API_KEY"] = "..."
# os.environ["SERPER_API_KEY"] = "..."
# os.environ["OPENAI_API_KEY"] = "..."

from dotenv import load_dotenv

load_dotenv()

from crewai import Agent
from crewai_tools.tools import WebsiteSearchTool, SerperDevTool, FileReadTool
Expand Down Expand Up @@ -126,7 +130,7 @@ def industry_analysis_task(self, agent, company_domain, company_description):
from crewai import Crew
import agentops

agentops.init(tags=["crew-job-posting-example"])
agentops.init(default_tags=["crew-job-posting-example"])

tasks = Tasks()
agents = Agents()
Expand Down
44 changes: 25 additions & 19 deletions examples/crew/markdown_validator.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@
}
],
"source": [
"%pip install -U crewai \n",
"%pip install -U crewai\n",
"%pip install -U agentops\n",
"%pip install -U python-dotenv\n",
"%pip install -U langchain_openai\n",
Expand Down Expand Up @@ -400,7 +400,7 @@
},
"outputs": [],
"source": [
"agentops.init(AGENTOPS_API_KEY, default_tags=['markdown_validator'])"
"agentops.init(AGENTOPS_API_KEY, default_tags=[\"markdown_validator\"])"
]
},
{
Expand All @@ -424,7 +424,7 @@
" A tool to review files for markdown syntax errors.\n",
"\n",
" Returns:\n",
" - validation_results: A list of validation results \n",
" - validation_results: A list of validation results\n",
" and suggestions on how to fix them.\n",
" \"\"\"\n",
"\n",
Expand All @@ -439,7 +439,7 @@
" return results # Return the reviewed document\n",
" except PyMarkdownApiException as this_exception:\n",
" print(f\"API Exception: {this_exception}\", file=sys.stderr)\n",
" return f\"API Exception: {str(this_exception)}\"\n"
" return f\"API Exception: {str(this_exception)}\""
]
},
{
Expand All @@ -455,10 +455,12 @@
" model_name=\"llama3-70b-8192\",\n",
")\n",
"\n",
"default_llm = ChatOpenAI(openai_api_base=os.environ.get(\"OPENAI_API_BASE_URL\", \"https://api.openai.com/v1\"),\n",
" openai_api_key=OPENAI_API_KEY,\n",
" temperature=0.1, \n",
" model_name=os.environ.get(\"MODEL_NAME\", \"gpt-3.5-turbo\"))"
"default_llm = ChatOpenAI(\n",
" openai_api_base=os.environ.get(\"OPENAI_API_BASE_URL\", \"https://api.openai.com/v1\"),\n",
" openai_api_key=OPENAI_API_KEY,\n",
" temperature=0.1,\n",
" model_name=os.environ.get(\"MODEL_NAME\", \"gpt-3.5-turbo\"),\n",
")"
]
},
{
Expand Down Expand Up @@ -486,22 +488,24 @@
"metadata": {},
"outputs": [],
"source": [
"general_agent = Agent(role='Requirements Manager',\n",
" goal=\"\"\"Provide a detailed list of the markdown \n",
"general_agent = Agent(\n",
" role=\"Requirements Manager\",\n",
" goal=\"\"\"Provide a detailed list of the markdown \n",
" linting results. Give a summary with actionable \n",
" tasks to address the validation results. Write your \n",
" response as if you were handing it to a developer \n",
" to fix the issues.\n",
" DO NOT provide examples of how to fix the issues or\n",
" recommend other tools to use.\"\"\",\n",
" backstory=\"\"\"You are an expert business analyst \n",
" backstory=\"\"\"You are an expert business analyst \n",
"\t\t\t\t\tand software QA specialist. You provide high quality, \n",
" thorough, insightful and actionable feedback via \n",
" detailed list of changes and actionable tasks.\"\"\",\n",
" allow_delegation=False, \n",
" verbose=True,\n",
" tools=[markdown_validation_tool],\n",
" llm=default_llm) #groq_llm) "
" allow_delegation=False,\n",
" verbose=True,\n",
" tools=[markdown_validation_tool],\n",
" llm=default_llm,\n",
") # groq_llm)"
]
},
{
Expand All @@ -519,7 +523,8 @@
"metadata": {},
"outputs": [],
"source": [
"syntax_review_task = Task(description=f\"\"\"\n",
"syntax_review_task = Task(\n",
" description=f\"\"\"\n",
" Use the markdown_validation_tool to review \n",
" the file(s) at this path: {filename}\n",
" \n",
Expand All @@ -539,8 +544,9 @@
" \n",
" If you already know the answer or if you do not need \n",
" to use a tool, return it as your Final Answer.\"\"\",\n",
" agent=general_agent,\n",
" expected_output=\"\")"
" agent=general_agent,\n",
" expected_output=\"\",\n",
")"
]
},
{
Expand Down Expand Up @@ -576,7 +582,7 @@
"metadata": {},
"outputs": [],
"source": [
"agentops.end_session('Success')"
"agentops.end_session(\"Success\")"
]
}
],
Expand Down
Loading

0 comments on commit a12b59a

Please sign in to comment.