Skip to content

Commit

Permalink
Merge branch 'main' into feat-add-anthropic-messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aflament committed Mar 14, 2024
2 parents a3ee225 + 45a260c commit 1811a09
Show file tree
Hide file tree
Showing 39 changed files with 292 additions and 128 deletions.
1 change: 1 addition & 0 deletions council/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Init file."""

from .agents import Agent, AgentChain, AgentResult
from .chains import Chain, ChainBase
from .contexts import AgentContext, Budget, ChainContext, ChatHistory, ChatMessage, LLMContext, SkillContext
Expand Down
4 changes: 3 additions & 1 deletion council/agent_tests/agent_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import time
from enum import Enum
from typing import List, Dict, Any, Sequence, Optional
Expand Down Expand Up @@ -186,7 +188,7 @@ def __init__(self, test_cases: Optional[List[AgentTestCase]] = None):
else:
self._test_cases = []

def add_test_case(self, prompt: str, scorers: List[ScorerBase]) -> "AgentTestSuite":
def add_test_case(self, prompt: str, scorers: List[ScorerBase]) -> AgentTestSuite:
self._test_cases.append(AgentTestCase(prompt, scorers))
return self

Expand Down
6 changes: 4 additions & 2 deletions council/agents/agent.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import itertools
from concurrent import futures
from typing import Dict, List, Optional, Sequence
Expand Down Expand Up @@ -165,7 +167,7 @@ def _execute_unit(iteration_context: AgentContext, unit: ExecutionUnit):
context.logger.info(f'message="chain execution ended" chain="{chain.name}" execution_unit="{unit.name}"')

@staticmethod
def from_skill(skill: SkillBase, chain_description: Optional[str] = None) -> "Agent":
def from_skill(skill: SkillBase, chain_description: Optional[str] = None) -> Agent:
"""
Helper function to create a new agent with a :class:`.BasicController`, a
:class:`.BasicEvaluator` and a single :class:`.SkillBase` wrapped into a :class:`.Chain`
Expand All @@ -182,7 +184,7 @@ def from_skill(skill: SkillBase, chain_description: Optional[str] = None) -> "Ag
@staticmethod
def from_chain(
chain: ChainBase, evaluator: EvaluatorBase = BasicEvaluator(), filter: FilterBase = BasicFilter()
) -> "Agent":
) -> Agent:
"""
Helper function to create a new agent with a :class:`.BasicController`, a
:class:`.BasicEvaluator` and a single :class:`.SkillBase` wrapped into a :class:`.Chain`
Expand Down
16 changes: 9 additions & 7 deletions council/contexts/_agent_context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Iterable, Optional, Sequence

from ._agent_context_store import AgentContextStore
Expand All @@ -15,11 +17,11 @@ class AgentContext(ContextBase):
the execution context given to an :class:`~council.agents.Agent`
"""

def __init__(self, store: AgentContextStore, execution_context: ExecutionContext, budget: Budget):
def __init__(self, store: AgentContextStore, execution_context: ExecutionContext, budget: Budget) -> None:
super().__init__(store, execution_context, budget)

@staticmethod
def empty(budget: Optional[Budget] = None) -> "AgentContext":
def empty(budget: Optional[Budget] = None) -> AgentContext:
"""
creates a new instance with no data
Expand All @@ -29,7 +31,7 @@ def empty(budget: Optional[Budget] = None) -> "AgentContext":
return AgentContext.from_chat_history(ChatHistory(), budget)

@staticmethod
def from_chat_history(chat_history: ChatHistory, budget: Optional[Budget] = None) -> "AgentContext":
def from_chat_history(chat_history: ChatHistory, budget: Optional[Budget] = None) -> AgentContext:
"""
creates a new instance from a :class:`ChatHistory`
Expand All @@ -41,7 +43,7 @@ def from_chat_history(chat_history: ChatHistory, budget: Optional[Budget] = None
return AgentContext(store, ExecutionContext(store.execution_log, "agent"), budget or Budget.default())

@staticmethod
def from_user_message(message: str, budget: Optional[Budget] = None) -> "AgentContext":
def from_user_message(message: str, budget: Optional[Budget] = None) -> AgentContext:
"""
creates a new instance from a user message.
The :class:`ChatHistory` contains only the given message
Expand All @@ -52,7 +54,7 @@ def from_user_message(message: str, budget: Optional[Budget] = None) -> "AgentCo
"""
return AgentContext.from_chat_history(ChatHistory.from_user_message(message), budget)

def new_agent_context_for(self, monitored: Monitored) -> "AgentContext":
def new_agent_context_for(self, monitored: Monitored) -> AgentContext:
"""
creates a new instance for the given object, adjusting the execution context appropriately
Expand All @@ -67,15 +69,15 @@ def new_iteration(self) -> None:
"""
self._store.new_iteration()

def new_agent_context_for_new_iteration(self) -> "AgentContext":
def new_agent_context_for_new_iteration(self) -> AgentContext:
"""
creates a new instance, adjusting the execution context appropriately
"""
self.new_iteration()
name = f"iterations[{len(self._store.iterations) - 1}]"
return AgentContext(self._store, self._execution_context.new_from_name(name), self._budget)

def new_agent_context_for_execution_unit(self, name: str) -> "AgentContext":
def new_agent_context_for_execution_unit(self, name: str) -> AgentContext:
"""
creates a new instance, adjusting the execution context for the given name
Expand Down
20 changes: 16 additions & 4 deletions council/contexts/_budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Consumption:
"""

def __init__(self, value: float, unit: str, kind: str):
def __init__(self, value: float, unit: str, kind: str) -> None:
"""
Initializes a Consumption instance.
Expand Down Expand Up @@ -54,16 +54,28 @@ def kind(self) -> str:
def __str__(self):
return f"{self._kind} consumption: {self._value} {self.unit}"

def add(self, value: float) -> "Consumption":
def add(self, value: float) -> Consumption:
"""
Returns a new Consumption instance with the value incremented by the specified value.
"""
return Consumption(self._value + value, self.unit, self._kind)

def subtract(self, value: float) -> "Consumption":
def subtract(self, value: float) -> Consumption:
"""
Returns a new Consumption instance with the value decremented by the specified value.
"""
return Consumption(self._value - value, self.unit, self._kind)

def add_value(self, value: float) -> None:
"""
Increments the value of the consumption by the specified value.
"""
self._value += value

def subtract_value(self, value: float) -> None:
"""
Decrements the value of the consumption by the specified value.
"""
self._value -= value

def to_dict(self) -> Dict[str, Any]:
Expand Down Expand Up @@ -160,7 +172,7 @@ def __repr__(self):
return f"Budget({self._duration})"

@staticmethod
def default() -> "Budget":
def default() -> Budget:
"""
Helper function that create a new Budget with a default value.
Expand Down
10 changes: 6 additions & 4 deletions council/contexts/_chain_context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Iterable, List, Optional

import more_itertools
Expand Down Expand Up @@ -106,7 +108,7 @@ def from_agent_context(context: AgentContext, monitored: Monitored, name: str, b
context._store, context._execution_context.new_for(monitored), name, budget or Budget.default()
)

def fork_for(self, monitored: Monitored, budget: Optional[Budget] = None) -> "ChainContext":
def fork_for(self, monitored: Monitored, budget: Optional[Budget] = None) -> ChainContext:
"""
forks the context for the given object, adjust the execution context appropriately
"""
Expand Down Expand Up @@ -156,7 +158,7 @@ def extend(self, messages: Iterable[ChatMessage]) -> None:
self.append(message)

@staticmethod
def from_chat_history(history: ChatHistory, budget: Optional[Budget] = None) -> "ChainContext":
def from_chat_history(history: ChatHistory, budget: Optional[Budget] = None) -> ChainContext:
"""
helper function that creates a new instance from a :class:`ChatHistory`.
Expand All @@ -169,14 +171,14 @@ def from_chat_history(history: ChatHistory, budget: Optional[Budget] = None) ->
return ChainContext.from_agent_context(context, MockMonitored("mock chain"), "mock chain", budget)

@staticmethod
def from_user_message(message: str, budget: Optional[Budget] = None) -> "ChainContext":
def from_user_message(message: str, budget: Optional[Budget] = None) -> ChainContext:
"""
creates a new instance from a user message. The :class:`ChatHistory` contains only the user message
"""
return ChainContext.from_chat_history(ChatHistory.from_user_message(message), budget)

@staticmethod
def empty() -> "ChainContext":
def empty() -> ChainContext:
"""
helper function that creates a new empty instance.
Expand Down
3 changes: 2 additions & 1 deletion council/contexts/_chat_history.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from ._message_list import MessageList


Expand All @@ -7,7 +8,7 @@ class ChatHistory(MessageList):
"""

@staticmethod
def from_user_message(message: str) -> "ChatHistory":
def from_user_message(message: str) -> ChatHistory:
"""
helpers function that returns a new instance containing one user message
"""
Expand Down
10 changes: 6 additions & 4 deletions council/contexts/_chat_message.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from enum import Enum
from typing import Any, Dict

Expand Down Expand Up @@ -60,31 +62,31 @@ def __init__(self, message: str, kind: ChatMessageKind, data: Any = None, source
self._is_error = is_error

@staticmethod
def agent(message: str, data: Any = None, source: str = "", is_error: bool = False) -> "ChatMessage":
def agent(message: str, data: Any = None, source: str = "", is_error: bool = False) -> ChatMessage:
"""
Helper function to create message of kind :attr:`ChatMessageKind.Agent`.
See :meth:`ChatMessage.__init__` for details
"""
return ChatMessage(message, ChatMessageKind.Agent, data, source, is_error)

@staticmethod
def user(message: str, data: Any = None, source: str = "", is_error: bool = False) -> "ChatMessage":
def user(message: str, data: Any = None, source: str = "", is_error: bool = False) -> ChatMessage:
"""
Helper function to create message of kind :attr:`ChatMessageKind.User`.
See :meth:`ChatMessage.__init__` for details
"""
return ChatMessage(message, ChatMessageKind.User, data, source, is_error)

@staticmethod
def skill(message: str, data: Any = None, source: str = "", is_error: bool = False) -> "ChatMessage":
def skill(message: str, data: Any = None, source: str = "", is_error: bool = False) -> ChatMessage:
"""
Helper function to create message of kind :attr:`ChatMessageKind.Skill`.
See :meth:`ChatMessage.__init__` for details
"""
return ChatMessage(message, ChatMessageKind.Skill, data, source, is_error)

@staticmethod
def chain(message: str, data: Any = None, source: str = "", is_error: bool = False) -> "ChatMessage":
def chain(message: str, data: Any = None, source: str = "", is_error: bool = False) -> ChatMessage:
"""
Helper function to create message of kind :attr:`ChatMessageKind.Chain`.
See :meth:`ChatMessage.__init__` for details
Expand Down
6 changes: 2 additions & 4 deletions council/contexts/_composite_message_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ def __init__(self, collections: List[MessageCollection]):
@property
def messages(self) -> Iterable[ChatMessage]:
for collection in self._collections:
for message in collection.messages:
yield message
yield from collection.messages

@property
def reversed(self) -> Iterable[ChatMessage]:
for collection in reversed(self._collections):
for message in collection.reversed:
yield message
yield from collection.reversed
7 changes: 4 additions & 3 deletions council/contexts/_execution_context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from typing import Optional

from ._monitorable import Monitorable
Expand All @@ -20,16 +21,16 @@ def __init__(
self._executionLog = execution_log or ExecutionLog()
self._entry = self._executionLog.new_entry(path, node)

def _new_path(self, name: str):
def _new_path(self, name: str) -> str:
return name if self._entry.source == "" else f"{self._entry.source}/{name}"

def new_from_name(self, name: str):
def new_from_name(self, name: str) -> ExecutionContext:
"""
returns a new instance for the given name
"""
return ExecutionContext(self._executionLog, self._new_path(name))

def new_for(self, monitored: Monitored) -> "ExecutionContext":
def new_for(self, monitored: Monitored) -> ExecutionContext:
"""
returns a new instance for the given object
"""
Expand Down
9 changes: 5 additions & 4 deletions council/contexts/_llm_context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from typing import Optional

from ._agent_context_store import AgentContextStore
Expand All @@ -13,26 +14,26 @@ class LLMContext(ContextBase):
represents a context used by a :class:`~council.llm.LLMBase`
"""

def __init__(self, store: AgentContextStore, execution_context: ExecutionContext, budget: Budget):
def __init__(self, store: AgentContextStore, execution_context: ExecutionContext, budget: Budget) -> None:
super().__init__(store, execution_context, budget)

@staticmethod
def from_context(context: ContextBase, monitored: Monitored, budget: Optional[Budget] = None) -> "LLMContext":
def from_context(context: ContextBase, monitored: Monitored, budget: Optional[Budget] = None) -> LLMContext:
"""
creates a new instance from the given context, adjusting the execution context appropriately
"""
return LLMContext(context._store, context._execution_context.new_for(monitored), budget or context._budget)

@staticmethod
def empty() -> "LLMContext":
def empty() -> LLMContext:
"""
helper function that creates a new empty instance
For test purpose only.
"""
return LLMContext(AgentContextStore(ChatHistory()), ExecutionContext(), InfiniteBudget())

def new_for(self, monitored: Monitored) -> "LLMContext":
def new_for(self, monitored: Monitored) -> LLMContext:
"""
returns a new instance for the given object, adjusting the execution context appropriately
"""
Expand Down
8 changes: 5 additions & 3 deletions council/contexts/_scorer_context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Optional

from ._agent_context_store import AgentContextStore
Expand All @@ -17,22 +19,22 @@ def __init__(self, store: AgentContextStore, execution_context: ExecutionContext
super().__init__(store, execution_context, budget)

@staticmethod
def from_context(context: ContextBase, monitored: Monitored, budget: Optional[Budget] = None) -> "ScorerContext":
def from_context(context: ContextBase, monitored: Monitored, budget: Optional[Budget] = None) -> ScorerContext:
"""
creates a new instance from the given context, adjusting the execution appropriately
"""
return ScorerContext(context._store, context._execution_context.new_for(monitored), budget or context._budget)

@staticmethod
def empty() -> "ScorerContext":
def empty() -> ScorerContext:
"""
helper function that creates a new empty instance
For test purpose only.
"""
return ScorerContext(AgentContextStore(ChatHistory()), ExecutionContext(), InfiniteBudget())

def new_for(self, monitored: Monitored) -> "ScorerContext":
def new_for(self, monitored: Monitored) -> ScorerContext:
"""
returns a new instance for the given object, adjusting the execution context appropriately
"""
Expand Down
8 changes: 5 additions & 3 deletions council/contexts/_skill_context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from typing import Any, Iterable

from council.utils import Option
Expand All @@ -13,7 +15,7 @@ class IterationContext:
Provides context information when running inside a loop.
"""

def __init__(self, index: int, value: Any):
def __init__(self, index: int, value: Any) -> None:
self._index = index
self._value = value

Expand Down Expand Up @@ -59,7 +61,7 @@ def __init__(
budget: Budget,
messages: Iterable[ChatMessage],
iteration: Option[IterationContext],
):
) -> None:
super().__init__(store, execution_context, name, budget, messages)
self._iteration = iteration

Expand All @@ -74,7 +76,7 @@ def iteration(self) -> Option[IterationContext]:
return self._iteration

@staticmethod
def from_chain_context(context: ChainContext, iteration: Option[IterationContext]) -> "SkillContext":
def from_chain_context(context: ChainContext, iteration: Option[IterationContext]) -> SkillContext:
return SkillContext(
context._store,
context._execution_context,
Expand Down
Loading

0 comments on commit 1811a09

Please sign in to comment.