agent: ditch aiohttp & define REQUESTS_CA_BUNDLE to fix http proxying / trust the self-signed cert from python

This commit is contained in:
ochafik 2024-10-24 06:35:37 +01:00
parent 0f5d63943f
commit d338bfb87f
6 changed files with 43 additions and 25 deletions

View file

@ -4,6 +4,7 @@ RUN python -m pip install --upgrade pip && \
apt clean cache apt clean cache
COPY requirements.txt /root/ COPY requirements.txt /root/
# COPY . /root/
WORKDIR /root WORKDIR /root
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
@ -13,5 +14,5 @@ COPY ./tools/*.py /root/tools/
COPY ./squid/ssl_cert/squidCA.crt /usr/local/share/ca-certificates/squidCA.crt COPY ./squid/ssl_cert/squidCA.crt /usr/local/share/ca-certificates/squidCA.crt
RUN chmod 644 /usr/local/share/ca-certificates/squidCA.crt && update-ca-certificates RUN chmod 644 /usr/local/share/ca-certificates/squidCA.crt && update-ca-certificates
# ENTRYPOINT [ "python" ] ENTRYPOINT [ "uvicorn" ]
# CMD ["serve_tools.py"] CMD ["serve_tools:app", "--host", "0.0.0.0", "--port", "8088"]

View file

@ -32,12 +32,11 @@ services:
networks: networks:
- private_net - private_net
environment: environment:
- PORT=8088 - VERBOSE=1
- BRAVE_SEARCH_API_KEY=${BRAVE_SEARCH_API_KEY} - BRAVE_SEARCH_API_KEY=${BRAVE_SEARCH_API_KEY}
- REQUESTS_CA_BUNDLE=/usr/local/share/ca-certificates/squidCA.crt
- http_proxy=http://outgoing_proxy:3128 - http_proxy=http://outgoing_proxy:3128
- https_proxy=http://outgoing_proxy:3128 - https_proxy=http://outgoing_proxy:3128
entrypoint: python
command: serve_tools.py
# entrypoint: /usr/bin/bash # entrypoint: /usr/bin/bash
# command: ["-c", "pip install --upgrade gguf && apt update && apt install -y curl && curl https://ochafik.com && pip install gguf"] # command: ["-c", "pip install --upgrade gguf && apt update && apt install -y curl && curl https://ochafik.com && pip install gguf"]

View file

@ -12,6 +12,7 @@
uv run examples/agent/serve_tools.py --port 8088 uv run examples/agent/serve_tools.py --port 8088
''' '''
import asyncio
import logging import logging
import re import re
import fastapi import fastapi
@ -24,6 +25,11 @@ from tools.fetch import fetch_page
from tools.search import brave_search from tools.search import brave_search
from tools.python import python, python_tools from tools.python import python, python_tools
# try:
# # https://github.com/aio-libs/aiohttp/discussions/6044
# setattr(asyncio.sslproto._SSLProtocolTransport, "_start_tls_compatible", True) # type: ignore
# except Exception as e:
# print(f'Failed to patch asyncio: {e}', file=sys.stderr)
verbose = os.environ.get('VERBOSE', '0') == '1' verbose = os.environ.get('VERBOSE', '0') == '1'
include = os.environ.get('INCLUDE_TOOLS') include = os.environ.get('INCLUDE_TOOLS')

View file

@ -23,7 +23,7 @@ http_access allow all
refresh_pattern -i ($|\.)(files\.pythonhosted\.org|pypi\.org)/.*?\.(whl|zip|tar\.gz)$ 10080 90% 43200 reload-into-ims refresh_pattern -i ($|\.)(files\.pythonhosted\.org|pypi\.org)/.*?\.(whl|zip|tar\.gz)$ 10080 90% 43200 reload-into-ims
# Cache Debian packages # Cache Debian packages
refresh_pattern \.debian\.org/.*?\.(deb|udeb|tar\.(gz|xz|bz2)$ 129600 100% 129600 refresh_pattern \.debian\.org/.*?\.(deb|udeb|tar\.(gz|xz|bz2))$ 129600 100% 129600
# Configure cache # Configure cache
cache_dir ufs /var/spool/squid 10000 16 256 cache_dir ufs /var/spool/squid 10000 16 256

View file

@ -1,6 +1,7 @@
import aiohttp # import aiohttp
import html2text import html2text
import logging import logging
import requests
async def fetch_page(url: str): async def fetch_page(url: str):
@ -10,11 +11,16 @@ async def fetch_page(url: str):
try: try:
logging.debug(f'[fetch_page] Fetching %s', url) logging.debug(f'[fetch_page] Fetching %s', url)
async with aiohttp.ClientSession() as session: response = requests.get(url)
async with session.get(url) as res: response.raise_for_status()
res.raise_for_status() content = response.text
content = await res.text() # async with aiohttp.ClientSession(trust_env=True) as session:
except aiohttp.ClientError as e: # async with session.get(url) as res:
# res.raise_for_status()
# content = await res.text()
# except aiohttp.ClientError as e:
# raise Exception(f'Failed to fetch {url}: {e}')
except requests.exceptions.RequestException as e:
raise Exception(f'Failed to fetch {url}: {e}') raise Exception(f'Failed to fetch {url}: {e}')
# NOTE: Pyppeteer doesn't work great in docker, short of installing a bunch of dependencies # NOTE: Pyppeteer doesn't work great in docker, short of installing a bunch of dependencies

View file

@ -1,13 +1,13 @@
import sys # import aiohttp
from pydantic import Field
import aiohttp
import itertools import itertools
import json import json
import logging import logging
import os import os
from typing import Annotated, Dict, List from typing import Dict, List
import urllib.parse import urllib.parse
import requests
def _extract_values(keys, obj): def _extract_values(keys, obj):
values = {} values = {}
@ -66,12 +66,18 @@ async def brave_search(*, query: str) -> List[Dict]:
for r in results_of_type: for r in results_of_type:
yield _extract_values(keys, r) yield _extract_values(keys, r)
async with aiohttp.ClientSession() as session: res = requests.get(url, headers=headers)
async with session.get(url, headers=headers) as res:
if not res.ok: if not res.ok:
raise Exception(await res.text()) raise Exception(res.text)
reponse = res.json()
res.raise_for_status() res.raise_for_status()
response = await res.json() response = res.text
# async with aiohttp.ClientSession(trust_env=True) as session:
# async with session.get(url, headers=headers) as res:
# if not res.ok:
# raise Exception(await res.text())
# res.raise_for_status()
# response = await res.json()
results = list(itertools.islice(extract_results(response), max_results)) results = list(itertools.islice(extract_results(response), max_results))
print(json.dumps(dict(query=query, response=response, results=results), indent=2)) print(json.dumps(dict(query=query, response=response, results=results), indent=2))