Skip to content

Self improving agents #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: human-seeded-evals
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.12
3.13
16 changes: 12 additions & 4 deletions human-seeded-evals/app/agent.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
from __future__ import annotations as _annotations

import os
from dataclasses import dataclass
from datetime import datetime

from pydantic_ai import Agent, RunContext

from .models import TimeRangeInputs, TimeRangeResponse
from .self_improving_agent import Coach, SelfImprovingAgentModel


@dataclass
class TimeRangeDeps:
now: datetime


instrunctions = "Convert the user's request into a structured time range."
system_prompt = "Convert the user's request into a structured time range."
time_range_agent = Agent[TimeRangeDeps, TimeRangeResponse](
'anthropic:claude-sonnet-4-0',
output_type=TimeRangeResponse, # type: ignore # we can't yet annotate something as receiving a TypeForm
deps_type=TimeRangeDeps,
instructions=instrunctions,
system_prompt=system_prompt,
retries=1,
)


@time_range_agent.instructions
def get_coach() -> Coach:
logfire_read_token = os.environ['LOGFIRE_READ_TOKEN']
return Coach('time_range_agent', logfire_read_token)


@time_range_agent.tool
def inject_current_time(ctx: RunContext[TimeRangeDeps]) -> str:
"""Add the user's current time and timezone in the format 'Friday, November 22, 2024 11:15:14 PST' to context."""
return f"The user's current time is {ctx.deps.now:%A, %B %d, %Y %H:%M:%S %Z}."


async def infer_time_range(inputs: TimeRangeInputs) -> TimeRangeResponse:
"""Infer a time range from a user prompt."""
result = await time_range_agent.run(inputs.prompt, deps=TimeRangeDeps(now=inputs.now))
model = SelfImprovingAgentModel('anthropic:claude-sonnet-4-0')
result = await time_range_agent.run(inputs.prompt, deps=TimeRangeDeps(now=inputs.now), model=model)
return result.output
43 changes: 42 additions & 1 deletion human-seeded-evals/app/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
from datetime import datetime, timezone

import logfire
from fastapi import FastAPI
from pydantic import BaseModel

from .agent import infer_time_range
from .agent import get_coach, infer_time_range
from .models import TimeRangeInputs, TimeRangeResponse
from .self_improving_agent import ModelContextPatch

logfire.configure(environment='dev')

logfire.instrument_pydantic_ai()
logfire.instrument_httpx(capture_all=True)
coach = get_coach()


app = FastAPI()
logfire.instrument_fastapi(app)
Expand All @@ -14,3 +22,36 @@
@app.post('/api/timerange')
async def convert_time_range(time_range_inputs: TimeRangeInputs) -> TimeRangeResponse:
return await infer_time_range(time_range_inputs)


class Field(BaseModel):
id: str
text: str


@app.get('/api/context')
def get_agent_context() -> list[Field]:
coach_fields = coach.get_fields() or []
fields = [Field(id=f.key, text=f.current_prompt or '') for f in coach_fields]

if patch := coach.get_patch():
for field in fields:
if new_text := patch.context_patch.get(field.id):
field.text = new_text

return fields


class PostFields(BaseModel):
fields: list[Field]


@app.post('/api/context')
def post_agent_context(m: PostFields):
context_patch = {f.id: f.text for f in m.fields if f.text}
coach.update_patch(ModelContextPatch(context_patch=context_patch, timestamp=datetime.now(tz=timezone.utc)))


@app.post('/api/context/update')
async def post_update_agent_context():
await coach.run()
Loading