ccm commited on
Commit
d88717c
·
1 Parent(s): dcdf0b5

Revert "Adding a second agent"

Browse files

This reverts commit dcdf0b5d9d3d0ad2168ef2ca6438949dade7f97a.

Dockerfile CHANGED
@@ -15,7 +15,7 @@ RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-ins
15
  RUN python3 -m pip install --no-cache-dir --upgrade pip && \
16
  pip3 install --no-cache-dir \
17
  fastapi uvicorn httpx[http2] \
18
- smolagents[toolkit] litellm \
19
  "pydantic>=2,<3"
20
 
21
  # MongoDB
 
15
  RUN python3 -m pip install --no-cache-dir --upgrade pip && \
16
  pip3 install --no-cache-dir \
17
  fastapi uvicorn httpx[http2] \
18
+ smolagents litellm \
19
  "pydantic>=2,<3"
20
 
21
  # MongoDB
agents/{code_writing_agent_without_tools.py → code_writing_agent.py} RENAMED
@@ -3,7 +3,7 @@ import smolagents
3
  import smolagents.models
4
 
5
 
6
- def generate_code_writing_agent_without_tools() -> smolagents.CodeAgent:
7
  return smolagents.CodeAgent(
8
  name="code_writing_agent_without_tools",
9
  model=smolagents.models.OpenAIServerModel(
 
3
  import smolagents.models
4
 
5
 
6
+ def create_code_writing_agent():
7
  return smolagents.CodeAgent(
8
  name="code_writing_agent_without_tools",
9
  model=smolagents.models.OpenAIServerModel(
agents/code_writing_agent_with_tools.py DELETED
@@ -1,23 +0,0 @@
1
- import os
2
- import smolagents
3
- import smolagents.models
4
-
5
-
6
- def generate_code_writing_agent_with_tools() -> smolagents.CodeAgent:
7
- return smolagents.CodeAgent(
8
- name="code_writing_agent_with_tools",
9
- model=smolagents.models.OpenAIServerModel(
10
- model_id=os.getenv("AGENT_MODEL", ""),
11
- api_base=os.getenv("UPSTREAM_OPENAI_BASE", "").rstrip("/"),
12
- api_key=os.getenv("OPENAI_API_KEY"),
13
- ),
14
- tools=[
15
- smolagents.DuckDuckGoSearchTool(max_results=5),
16
- smolagents.VisitWebpageTool(max_output_length=5000),
17
- ], # no extra tools
18
- add_base_tools=False,
19
- max_steps=4,
20
- verbosity_level=int(
21
- os.getenv("AGENT_VERBOSITY", "1")
22
- ), # quieter by default; override via env
23
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
proxy.py CHANGED
@@ -2,14 +2,14 @@
2
  OpenAI-compatible FastAPI proxy that wraps a smolagents CodeAgent
3
  """
4
 
5
- import os
6
- import re
7
- import json
8
- import time
9
- import asyncio
10
- import typing
11
- import logging
12
- import threading
13
 
14
  import fastapi
15
  import fastapi.responses
@@ -19,13 +19,7 @@ import contextlib
19
  # Upstream pass-through
20
  import httpx
21
 
22
- from agents.code_writing_agent_without_tools import (
23
- generate_code_writing_agent_without_tools,
24
- )
25
-
26
- from agents.code_writing_agent_with_tools import (
27
- generate_code_writing_agent_with_tools,
28
- )
29
 
30
  # Logging setup
31
  logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO").upper())
@@ -393,6 +387,7 @@ async def run_agent_stream(task: str, agent_obj: typing.Optional[typing.Any] = N
393
  """
394
  loop = asyncio.get_running_loop()
395
  q: asyncio.Queue = asyncio.Queue()
 
396
 
397
  stop_evt = threading.Event()
398
 
@@ -411,8 +406,8 @@ async def run_agent_stream(task: str, agent_obj: typing.Optional[typing.Any] = N
411
  except Exception:
412
  # Fallbacks: different names across versions
413
  steps = (
414
- getattr(agent_obj, "steps", [])
415
- or getattr(agent_obj, "memory", [])
416
  or []
417
  )
418
  if steps is None:
@@ -440,9 +435,11 @@ async def run_agent_stream(task: str, agent_obj: typing.Optional[typing.Any] = N
440
  qwriter
441
  ):
442
  used_iterable = False
443
- if hasattr(agent_obj, "run") and callable(getattr(agent_obj, "run")):
 
 
444
  try:
445
- res = agent_obj.run(task, stream=True)
446
  if hasattr(res, "__iter__") and not isinstance(
447
  res, (str, bytes)
448
  ):
@@ -469,7 +466,7 @@ async def run_agent_stream(task: str, agent_obj: typing.Optional[typing.Any] = N
469
  "stream_run",
470
  "run_with_callback",
471
  ):
472
- fn = getattr(agent_obj, name, None)
473
  if callable(fn):
474
  try:
475
  res = fn(task)
@@ -502,16 +499,16 @@ async def run_agent_stream(task: str, agent_obj: typing.Optional[typing.Any] = N
502
 
503
  if final_result is None and not used_iterable:
504
  # Last resort: synchronous run()/generate()/callable
505
- if hasattr(agent_obj, "run") and callable(
506
- getattr(agent_obj, "run")
507
  ):
508
- final_result = agent_obj.run(task)
509
- elif hasattr(agent_obj, "generate") and callable(
510
- getattr(agent_obj, "generate")
511
  ):
512
- final_result = agent_obj.generate(task)
513
- elif callable(agent_obj):
514
- final_result = agent_obj(task)
515
 
516
  except Exception as e:
517
  try:
@@ -692,14 +689,17 @@ async def chat_completions(req: fastapi.Request):
692
  # Per-request agent override if a custom model id was provided (different from defaults)
693
  agent_for_request = None
694
  if model_name not in (
 
695
  AGENT_MODEL,
696
  AGENT_MODEL + "-nothink",
697
  ) and isinstance(model_name, str):
698
- if model_name is "code-writing-agent-with-tools":
699
- agent_for_request = generate_code_writing_agent_with_tools()
700
-
701
- if model_name is "code-writing-agent-without-tools":
702
- agent_for_request = generate_code_writing_agent_without_tools()
 
 
703
 
704
  try:
705
  if stream:
 
2
  OpenAI-compatible FastAPI proxy that wraps a smolagents CodeAgent
3
  """
4
 
5
+ import os # For dealing with env vars
6
+ import re # For tag stripping
7
+ import json # For JSON handling
8
+ import time # For timestamps and sleeps
9
+ import asyncio # For async operations
10
+ import typing # For type annotations
11
+ import logging # For logging
12
+ import threading # For threading operations
13
 
14
  import fastapi
15
  import fastapi.responses
 
19
  # Upstream pass-through
20
  import httpx
21
 
22
+ from agents.code_writing_agent import create_code_writing_agent
 
 
 
 
 
 
23
 
24
  # Logging setup
25
  logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO").upper())
 
387
  """
388
  loop = asyncio.get_running_loop()
389
  q: asyncio.Queue = asyncio.Queue()
390
+ agent_to_use = agent_obj or create_code_writing_agent()
391
 
392
  stop_evt = threading.Event()
393
 
 
406
  except Exception:
407
  # Fallbacks: different names across versions
408
  steps = (
409
+ getattr(agent_to_use, "steps", [])
410
+ or getattr(agent_to_use, "memory", [])
411
  or []
412
  )
413
  if steps is None:
 
435
  qwriter
436
  ):
437
  used_iterable = False
438
+ if hasattr(agent_to_use, "run") and callable(
439
+ getattr(agent_to_use, "run")
440
+ ):
441
  try:
442
+ res = agent_to_use.run(task, stream=True)
443
  if hasattr(res, "__iter__") and not isinstance(
444
  res, (str, bytes)
445
  ):
 
466
  "stream_run",
467
  "run_with_callback",
468
  ):
469
+ fn = getattr(agent_to_use, name, None)
470
  if callable(fn):
471
  try:
472
  res = fn(task)
 
499
 
500
  if final_result is None and not used_iterable:
501
  # Last resort: synchronous run()/generate()/callable
502
+ if hasattr(agent_to_use, "run") and callable(
503
+ getattr(agent_to_use, "run")
504
  ):
505
+ final_result = agent_to_use.run(task)
506
+ elif hasattr(agent_to_use, "generate") and callable(
507
+ getattr(agent_to_use, "generate")
508
  ):
509
+ final_result = agent_to_use.generate(task)
510
+ elif callable(agent_to_use):
511
+ final_result = agent_to_use(task)
512
 
513
  except Exception as e:
514
  try:
 
689
  # Per-request agent override if a custom model id was provided (different from defaults)
690
  agent_for_request = None
691
  if model_name not in (
692
+ "code-writing-agent-without-tools",
693
  AGENT_MODEL,
694
  AGENT_MODEL + "-nothink",
695
  ) and isinstance(model_name, str):
696
+ try:
697
+ agent_for_request = create_code_writing_agent()
698
+ except Exception:
699
+ log.exception(
700
+ "Failed to construct agent for model '%s'; using default", model_name
701
+ )
702
+ agent_for_request = None
703
 
704
  try:
705
  if stream: