Skip to content

Commit

Permalink
Partial project refactor (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrverdasca authored Dec 2, 2024
1 parent 410ad60 commit 4cc28b9
Show file tree
Hide file tree
Showing 23 changed files with 390 additions and 440 deletions.
11 changes: 1 addition & 10 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,15 @@ DATABASE_PASS=postgres
DATABASE_NAME=postgres
DATABASE_PORT=63045

GITHUB_OWNER=
GITHUB_REPO=
GITHUB_USERNAME=
GITHUB_ACCESS_TOKEN=

PROMTAIL_ACCESS_TOKEN=
PROMTAIL_ID=

CELERY_BROKER_URL=redis://127.0.0.1:6379
CELERY_BACKEND_URL=redis://127.0.0.1:6379

LOCAL_REPOS_PATH=
LOCAL_REPOSITORIES_PATH=

REDIS_HOST=redis
REDIS_PORT=6379

LOCAL_LLM_HOST=http://localhost:11434

# Labs main flow
EMBEDDINGS_MODEL=
LLM_MODEL=
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ services:
- labs-db
volumes:
- ./labs/:/app/
- ${LOCAL_REPOS_PATH}:/local-repos/
- ${LOCAL_REPOSITORIES_PATH}:/local-repos/

ollama:
image: ollama/ollama:latest
Expand Down
163 changes: 78 additions & 85 deletions labs/api/codemonkey_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import logging

from api.schemas import (
ApplyCodeChangesRequest,
CommitChangesRequest,
CreateBranchRequest,
CreatePullRequestRequest,
FindSimilarEmbeddingsRequest,
GetIssueRequest,
GetLLMResponseRequest,
PreparePromptAndContextRequest,
RunOnLocalRepoRequest,
RunOnRepoRequest,
VectorizeRepoToDatabaseRequest,
from api.schemas.codemonkey import (
ApplyCodeChangesSchema,
FindEmbeddingsSchema,
GithubRepositorySchema,
LLMReponseSchema,
LocalRepositoryShema,
PreparePromptContextSchema,
VectorizeRepositorySchema,
)
from api.schemas.github import BranchIssueSchema, CommitSchema, IssueSchema, PullRequestSchema
from asgiref.sync import sync_to_async
from decorators import async_time_and_log_function
from django.http import HttpRequest
Expand All @@ -23,136 +20,132 @@
commit_changes_task,
create_branch_task,
create_pull_request_task,
find_similar_embeddings_task,
find_embeddings_task,
get_issue_task,
get_llm_response_task,
prepare_prompt_and_context_task,
run_on_local_repo_task,
run_on_repo_task,
vectorize_repo_to_database_task,
run_on_local_repository_task,
run_on_repository_task,
vectorize_repository_task,
)

logger = logging.getLogger(__name__)

router = Router(tags=["codemonkey"])


@router.post("/run_on_repo")
@router.post("/run_on_repository")
@async_time_and_log_function
async def run_on_repo_endpoint(request: HttpRequest, run_on_repo: RunOnRepoRequest):
async def run_on_repository_endpoint(request: HttpRequest, run_on_repository: GithubRepositorySchema):
try:
run_on_repo_task(
token=run_on_repo.github_token,
repo_owner=run_on_repo.repo_owner,
repo_name=run_on_repo.repo_name,
issue_number=run_on_repo.issue_number,
username=run_on_repo.username,
original_branch=run_on_repo.original_branch,
run_on_repository_task(
token=run_on_repository.token,
repository_owner=run_on_repository.repository_owner,
repository_name=run_on_repository.repository_name,
issue_number=run_on_repository.issue_number,
username=run_on_repository.username,
original_branch=run_on_repository.original_branch,
)
except Exception as ex:
logger.exception("Internal server error")
raise HttpError(status_code=500, message="Internal server error: " + str(ex))


@router.post("/run_on_local_repo", response={200: None})
@router.post("/run_on_local_repository", response={200: None})
@async_time_and_log_function
async def run_on_local_repo_endpoint(request: HttpRequest, run_on_local_repo: RunOnLocalRepoRequest):
async def run_on_local_repository_endpoint(request: HttpRequest, run_on_local_repository: LocalRepositoryShema):
try:
run_on_local_repo_task(repo_path=run_on_local_repo.repo_path, issue_text=run_on_local_repo.issue_text)
run_on_local_repository_task(
repository_path=run_on_local_repository.repository_path, issue_text=run_on_local_repository.prompt
)
except Exception as ex:
logger.exception("Internal server error")
raise HttpError(status_code=500, message="Internal server error: " + str(ex))


@router.post("/get_issue")
@router.post("/vectorize_repository")
@async_time_and_log_function
async def get_issue_endpoint(request: HttpRequest, get_issue: GetIssueRequest):
return get_issue_task(
token=get_issue.github_token,
repo_owner=get_issue.repo_owner,
repo_name=get_issue.repo_name,
issue_number=get_issue.issue_number,
username=get_issue.username,
async def vectorize_repository_endpoint(request: HttpRequest, vectorize_repository: VectorizeRepositorySchema):
return await sync_to_async(vectorize_repository_task, thread_sensitive=True)(
repository_path=vectorize_repository.repository_path
)


@router.post("/create_branch")
@router.post("/find_embeddings")
@async_time_and_log_function
async def create_branch_endpoint(request: HttpRequest, create_branch: CreateBranchRequest):
return create_branch_task(
token=create_branch.github_token,
repo_owner=create_branch.repo_owner,
repo_name=create_branch.repo_name,
issue_number=create_branch.issue_number,
username=create_branch.username,
original_branch=create_branch.original_branch,
issue_title=create_branch.issue_title,
async def find_embeddings_endpoint(request: HttpRequest, find_embeddings: FindEmbeddingsSchema):
return await sync_to_async(find_embeddings_task, thread_sensitive=True)(
issue_body=find_embeddings.prompt, repository_path=find_embeddings.repository_path
)


@router.post("/vectorize_repo_to_database")
@router.post("/prepare_prompt_and_context")
@async_time_and_log_function
async def vectorize_repo_to_database_endpoint(
request: HttpRequest, vectorize_repo_to_database: VectorizeRepoToDatabaseRequest
):
return await sync_to_async(vectorize_repo_to_database_task, thread_sensitive=True)(
repo_destination=vectorize_repo_to_database.repo_destination
async def prepare_prompt_and_context_endpoint(request: HttpRequest, prepare_prompt_context: PreparePromptContextSchema):
return await sync_to_async(prepare_prompt_and_context_task, thread_sensitive=True)(
issue_body=prepare_prompt_context.prompt, embeddings=prepare_prompt_context.embeddings
)


@router.post("/find_similar_embeddings")
@router.post("/get_llm_response")
@async_time_and_log_function
async def find_similar_embeddings_endpoint(request: HttpRequest, find_similar_embeddings: FindSimilarEmbeddingsRequest):
return await sync_to_async(find_similar_embeddings_task, thread_sensitive=True)(
issue_body=find_similar_embeddings.issue_body, repo_destination=find_similar_embeddings.repo_destination
)
async def get_llm_response_endpoint(request: HttpRequest, llm_reponse: LLMReponseSchema):
return await sync_to_async(get_llm_response_task, thread_sensitive=True)(context=llm_reponse.context)


@router.post("/prepare_prompt_and_context")
@router.post("/apply_code_changes")
@async_time_and_log_function
async def prepare_prompt_and_context_endpoint(
request: HttpRequest, prepare_prompt_and_context: PreparePromptAndContextRequest
):
return await sync_to_async(prepare_prompt_and_context_task, thread_sensitive=True)(
issue_body=prepare_prompt_and_context.issue_body, embeddings=prepare_prompt_and_context.embeddings
)
async def apply_code_changes_endpoint(request: HttpRequest, apply_code_changes: ApplyCodeChangesSchema):
return await sync_to_async(apply_code_changes_task, thread_sensitive=True)(llm_response=apply_code_changes.changes)


@router.post("/get_llm_response")
@router.post("/get_issue")
@async_time_and_log_function
async def get_llm_response_endpoint(request: HttpRequest, get_llm_reponse: GetLLMResponseRequest):
return await sync_to_async(get_llm_response_task, thread_sensitive=True)(prepared_context=get_llm_reponse.context)
async def get_issue_endpoint(request: HttpRequest, issue: IssueSchema):
return get_issue_task(
token=issue.token,
repository_owner=issue.repository_owner,
repository_name=issue.repository_name,
username=issue.username,
issue_number=issue.issue_number,
)


@router.post("/apply_code_changes")
@router.post("/create_branch")
@async_time_and_log_function
async def apply_code_changes_endpoint(request: HttpRequest, apply_code_changes: ApplyCodeChangesRequest):
return await sync_to_async(apply_code_changes_task, thread_sensitive=True)(
llm_response=apply_code_changes.llm_response
async def create_branch_endpoint(request: HttpRequest, branch: BranchIssueSchema):
return create_branch_task(
token=branch.token,
repository_owner=branch.repository_owner,
repository_name=branch.repository_name,
username=branch.username,
issue_number=branch.issue_number,
original_branch=branch.original_branch,
issue_title=branch.issue_title,
)


@router.post("/commit_changes")
@async_time_and_log_function
async def commit_changes_endpoint(request: HttpRequest, commit_changes: CommitChangesRequest):
async def commit_changes_endpoint(request: HttpRequest, commit: CommitSchema):
return await sync_to_async(commit_changes_task, thread_sensitive=True)(
token=commit_changes.github_token,
repo_owner=commit_changes.repo_owner,
repo_name=commit_changes.repo_name,
username=commit_changes.username,
branch_name=commit_changes.branch_name,
files_modified=commit_changes.files,
token=commit.token,
repository_owner=commit.repository_owner,
repository_name=commit.repository_name,
username=commit.username,
branch_name=commit.branch_name,
files_modified=commit.files,
)


@router.post("/create_pull_request")
@async_time_and_log_function
async def create_pull_request_endpoint(request: HttpRequest, create_pull_request: CreatePullRequestRequest):
async def create_pull_request_endpoint(request: HttpRequest, pull_request: PullRequestSchema):
return await sync_to_async(create_pull_request_task, thread_sensitive=True)(
token=create_pull_request.github_token,
repo_owner=create_pull_request.repo_owner,
repo_name=create_pull_request.repo_name,
username=create_pull_request.username,
branch_name=create_pull_request.branch_name,
original_branch=create_pull_request.original_branch,
token=pull_request.token,
repository_owner=pull_request.repository_owner,
repository_name=pull_request.repository_name,
username=pull_request.username,
branch_name=pull_request.changes_branch_name,
original_branch=pull_request.base_branch_name,
)
Loading

0 comments on commit 4cc28b9

Please sign in to comment.