Skip to content

fix: remove MultiAIProvider #555

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

Merged
merged 11 commits into from
Feb 19, 2025
Merged
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
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ readme = "README.md"
# renovate: datasource=python-version depName=python
requires-python = ">=3.12, <3.14"
dependencies = [
"backoff==2.2.1",
"openai==1.63.2",
"tiktoken<1.0.0,>=0.5.1",
"tabulate>=0.9.0,<1.0.0",
Expand All @@ -31,14 +30,12 @@ dependencies = [
"plotly>=5.24.0,<7.0.0",
"humanize<5.0.0,>=4.10.0",
"pytest-snapshot>=0.9.0",
"anthropic==0.23.1",
"pyjson5==1.6.8",
"mini-racer>=0.12.4",
"rustworkx>=0.15.1",
"typing-extensions>=4.12.2",
"termcolor>=2.4.0",
"sentry-sdk==2.22.0",
"tenacity>=9.0.0",
"click>=8.1.7",
"requests>=2.32.3",
"lazy-object-proxy>=0.0.0",
Expand Down Expand Up @@ -72,6 +69,7 @@ dependencies = [
"neo4j",
"modal>=0.73.45",
"slack-sdk",
"langchain-anthropic>=0.3.7",
]

license = { text = "Apache-2.0" }
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/extensions/events/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Callable

import modal # deptry: ignore
from anthropic import BaseModel
from pydantic import BaseModel

from codegen.extensions.clients.linear import LinearClient
from codegen.extensions.events.interface import EventHandlerManagerProtocol
Expand All @@ -24,13 +24,13 @@
self.signing_secret = os.environ["LINEAR_SIGNING_SECRET"]
self.linear_team_id = os.environ["LINEAR_TEAM_ID"]
self.registered_handlers = {}

Check failure on line 27 in src/codegen/extensions/events/linear.py

View workflow job for this annotation

GitHub Actions / mypy

error: Need type annotation for "registered_handlers" (hint: "registered_handlers: dict[<type>, <type>] = ...") [var-annotated]
def subscribe_handler_to_webhook(self, web_url: str, event_name: str):
client = LinearClient(access_token=self.access_token)

Check failure on line 30 in src/codegen/extensions/events/linear.py

View workflow job for this annotation

GitHub Actions / mypy

error: Signature of "subscribe_handler_to_webhook" incompatible with supertype "EventHandlerManagerProtocol" [override]
result = client.register_webhook(team_id=self.linear_team_id, webhook_url=web_url, enabled=True, resource_types=[event_name], secret=self.signing_secret)
return result

Check failure on line 32 in src/codegen/extensions/events/linear.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument "app_name" to "from_name" of "Function" has incompatible type "str | None"; expected "str" [arg-type]

Check failure on line 33 in src/codegen/extensions/events/linear.py

View workflow job for this annotation

GitHub Actions / mypy

error: Argument "webhook_url" to "register_webhook" of "LinearClient" has incompatible type "str | None"; expected "str" [arg-type]
def unsubscribe_handler_to_webhook(self, registered_handler: RegisteredWebhookHandler):
webhook_id = registered_handler.webhook_id

Expand All @@ -39,7 +39,7 @@
print(f"Unsubscribing from webhook {webhook_id}")
result = client.unregister_webhook(webhook_id)
return result
else:

Check failure on line 42 in src/codegen/extensions/events/linear.py

View workflow job for this annotation

GitHub Actions / mypy

error: Signature of "unsubscribe_handler_to_webhook" incompatible with supertype "EventHandlerManagerProtocol" [override]
print("No webhook id found for handler")
return None

Expand Down
24 changes: 18 additions & 6 deletions src/codegen/extensions/langchain/agent.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Demo implementation of an agent with Codegen tools."""

from langchain.agents import AgentExecutor
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain.agents.openai_functions_agent.base import OpenAIFunctionsAgent
from langchain.hub import pull
from langchain.tools import BaseTool
from langchain_anthropic import ChatAnthropic
from langchain_core.chat_history import InMemoryChatMessageHistory
from langchain_core.messages import BaseMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
Expand All @@ -29,7 +30,7 @@

def create_codebase_agent(
codebase: Codebase,
model_name: str = "gpt-4o",
model_name: str = "claude-3-5-sonnet-latest",
temperature: float = 0,
verbose: bool = True,
chat_history: list[BaseMessage] = [],
Expand All @@ -46,8 +47,13 @@ def create_codebase_agent(
Initialized agent with message history
"""
# Initialize language model
llm = ChatOpenAI(
model_name=model_name,
# llm = ChatOpenAI(
# model_name=model_name,
# temperature=temperature,
# )

llm = ChatAnthropic(
model="claude-3-5-sonnet-latest",
temperature=temperature,
)

Expand All @@ -64,7 +70,8 @@ def create_codebase_agent(
RevealSymbolTool(codebase),
SemanticEditTool(codebase),
SemanticSearchTool(codebase),
# CommitTool(codebase),
# =====[ Github Integration ]=====
# Enable Github integration
# GithubCreatePRTool(codebase),
# GithubViewPRTool(codebase),
# GithubCreatePRCommentTool(codebase),
Expand Down Expand Up @@ -128,7 +135,12 @@ def create_codebase_agent(
)

# Create the agent
agent = OpenAIFunctionsAgent(
# agent = OpenAIFunctionsAgent(
# llm=llm,
# tools=tools,
# prompt=prompt,
# )
agent = create_tool_calling_agent(
llm=llm,
tools=tools,
prompt=prompt,
Expand Down
7 changes: 1 addition & 6 deletions src/codegen/extensions/tools/reveal_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
import tiktoken

from codegen import Codebase
from codegen.sdk.ai.utils import count_tokens
from codegen.sdk.core.external_module import ExternalModule
from codegen.sdk.core.import_resolution import Import
from codegen.sdk.core.symbol import Symbol


def count_tokens(text: str) -> int:
"""Count the number of tokens in a string using GPT tokenizer."""
enc = tiktoken.get_encoding("cl100k_base") # GPT-4 encoding
return len(enc.encode(text))


def truncate_source(source: str, max_tokens: int) -> str:
"""Truncate source code to fit within max_tokens while preserving meaning.

Expand Down
27 changes: 19 additions & 8 deletions src/codegen/extensions/tools/semantic_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import difflib
import re

from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

from codegen import Codebase

from .tool_prompts import _HUMAN_PROMPT_DRAFT_EDITOR, _SYSTEM_PROMPT_DRAFT_EDITOR
from .view_file import add_line_numbers


def generate_diff(original: str, modified: str) -> str:
Expand Down Expand Up @@ -117,15 +118,25 @@
original_content = file.content
original_lines = original_content.split("\n")

# Check if file is too large for full edit
MAX_LINES = 300
if len(original_lines) > MAX_LINES and start == 1 and end == -1:
return {

Check warning on line 124 in src/codegen/extensions/tools/semantic_edit.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/extensions/tools/semantic_edit.py#L122-L124

Added lines #L122 - L124 were not covered by tests
"error": (
f"File is {len(original_lines)} lines long. For files longer than {MAX_LINES} lines, "
"please specify a line range using start and end parameters. "
"You may need to make multiple targeted edits."
),
"status": "error",
"line_count": len(original_lines),
}

# Handle append mode
if start == -1 and end == -1:
try:
file.add_symbol_from_source(edit_content)
codebase.commit()

# Analyze changes for append
new_lines = file.content.split("\n")

return {"filepath": filepath, "content": file.content, "diff": generate_diff(original_content, file.content), "status": "success"}
except Exception as e:
msg = f"Failed to append content: {e!s}"
Expand All @@ -144,10 +155,10 @@
system_message = _SYSTEM_PROMPT_DRAFT_EDITOR
human_message = _HUMAN_PROMPT_DRAFT_EDITOR
prompt = ChatPromptTemplate.from_messages([system_message, human_message])
llm = ChatOpenAI(
model="gpt-4o",
llm = ChatAnthropic(

Check warning on line 158 in src/codegen/extensions/tools/semantic_edit.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/extensions/tools/semantic_edit.py#L158

Added line #L158 was not covered by tests
model="claude-3-5-sonnet-latest",
temperature=0,
max_tokens=10000,
max_tokens=5000,
)
chain = prompt | llm
response = chain.invoke({"original_file_section": original_file_section, "edit_content": edit_content})
Expand All @@ -173,4 +184,4 @@
file.edit(new_content)
codebase.commit()

return {"filepath": filepath, "diff": diff, "status": "success"}
return {"filepath": filepath, "diff": diff, "status": "success", "new_content": add_line_numbers(new_content)}

Check warning on line 187 in src/codegen/extensions/tools/semantic_edit.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/extensions/tools/semantic_edit.py#L187

Added line #L187 was not covered by tests
16 changes: 5 additions & 11 deletions src/codegen/extensions/tools/semantic_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,12 @@

# Format results with previews
formatted_results = []
for filepath, score in results:
try:
file = codebase.get_file(filepath)
preview = file.content[:preview_length].replace("\n", " ").strip()
if len(file.content) > preview_length:
preview += "..."
for file, score in results:
preview = file.content[:preview_length].replace("\n", " ").strip()
if len(file.content) > preview_length:
preview += "..."

Check warning on line 75 in src/codegen/extensions/tools/semantic_search.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/extensions/tools/semantic_search.py#L72-L75

Added lines #L72 - L75 were not covered by tests

formatted_results.append({"filepath": filepath, "score": float(score), "preview": preview})
except Exception as e:
# Skip files that can't be read
print(f"Warning: Could not read file {filepath}: {e}")
continue
formatted_results.append({"filepath": file.filepath, "score": float(score), "preview": preview})

Check warning on line 77 in src/codegen/extensions/tools/semantic_search.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/extensions/tools/semantic_search.py#L77

Added line #L77 was not covered by tests

return {"status": "success", "query": query, "results": formatted_results}

Expand Down
10 changes: 5 additions & 5 deletions src/codegen/gscli/generate/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from codegen.gscli.generate.runner_imports import _generate_runner_imports
from codegen.gscli.generate.system_prompt import get_system_prompt
from codegen.gscli.generate.utils import LanguageType, generate_builtins_file
from codegen.sdk.ai.helpers import AnthropicHelper
from codegen.sdk.ai.client import get_openai_client
from codegen.sdk.code_generation.changelog_generation import generate_changelog
from codegen.sdk.code_generation.codegen_sdk_codebase import get_codegen_sdk_codebase
from codegen.sdk.code_generation.doc_utils.generate_docs_json import generate_docs_json
Expand Down Expand Up @@ -201,9 +201,9 @@ def generate_codegen_sdk_docs(docs_dir: str) -> None:

@generate.command()
@click.option("--docs-dir", default="docs", required=False)
@click.option("--anthropic-key", required=True)
@click.option("--openai-key", required=True)
@click.option("--complete", is_flag=True, help="Generate a complete changelog for the codegen_sdk API")
def changelog(docs_dir: str, anthropic_key: str, complete: bool = False) -> None:
def changelog(docs_dir: str, openai_key: str, complete: bool = False) -> None:
"""Generate the changelog for the codegen_sdk API and update the changelog.mdx file"""
print(colored("Generating changelog", "green"))
header = """---
Expand All @@ -212,8 +212,8 @@ def changelog(docs_dir: str, anthropic_key: str, complete: bool = False) -> None
iconType: "solid"
---
"""
# Generate the changelog for the codegen_sdk API and update the changelog.mdx file
client = AnthropicHelper(anthropic_key=anthropic_key, cache=True, openai_anthropic_translation=False)

client = get_openai_client(openai_key)

if complete:
entire_release_history = generate_changelog(client)
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/runner/sandbox/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from codegen.sdk.codebase.config import CodebaseConfig, ProjectConfig, SessionOptions
from codegen.sdk.codebase.factory.codebase_factory import CodebaseType
from codegen.sdk.core.codebase import Codebase
from codegen.sdk.secrets import Secrets
from codegen.shared.compilation.string_to_code import create_execute_function_from_codeblock
from codegen.shared.configs.models.secrets import SecretsConfig
from codegen.shared.configs.session_configs import config
from codegen.shared.performance.stopwatch_utils import stopwatch

Expand Down Expand Up @@ -47,7 +47,7 @@
async def _build_graph(self) -> Codebase:
logger.info("> Building graph...")
projects = [ProjectConfig(programming_language=self.repo.language, repo_operator=self.op, base_path=self.repo.base_path, subdirectories=self.repo.subdirectories)]
secrets = Secrets(openai_key=config.secrets.openai_api_key)
secrets = SecretsConfig(openai_api_key=config.secrets.openai_api_key)

Check warning on line 50 in src/codegen/runner/sandbox/runner.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/runner/sandbox/runner.py#L50

Added line #L50 was not covered by tests
codebase_config = CodebaseConfig(secrets=secrets, feature_flags=config.feature_flags.codebase)
return Codebase(projects=projects, config=codebase_config)

Expand Down
5 changes: 5 additions & 0 deletions src/codegen/sdk/ai/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from openai import OpenAI


def get_openai_client(key: str) -> OpenAI:
return OpenAI(api_key=key)

Check warning on line 5 in src/codegen/sdk/ai/client.py

View check run for this annotation

Codecov / codecov/patch

src/codegen/sdk/ai/client.py#L5

Added line #L5 was not covered by tests
105 changes: 0 additions & 105 deletions src/codegen/sdk/ai/converters.py

This file was deleted.

Loading
Loading