Skip to content
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

feat: added observability for llm check #27

Merged
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
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ AZURE_API_VERSION=
ANTHROPIC_API_KEY=

## Google Gemini
GEMINI_API_KEY=
GEMINI_API_KEY=

## LITELLM OBSERVABILITY
SUPABASE_URL=
SUPABASE_KEY=
2 changes: 2 additions & 0 deletions api/github_helper/pull_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def process_pull_request(payload):
diff_text=diff_text,
pull_request_title=pr_title,
pull_request_desc=pr_description,
user=repo_name,
)
post_pull_request(comment_url, review_body, access_token)

Expand All @@ -54,6 +55,7 @@ def process_pr_desc(payload):
diff_text=diff_text,
pull_request_title=pr_title,
pull_request_desc=pr_description,
user=repo_name,
)
patch_pr_body(pr_url, desc, access_token)

Expand Down
7 changes: 0 additions & 7 deletions api/github_helper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import logging
import hmac
import hashlib
import json

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -61,9 +60,3 @@ def is_github_signature_valid(headers, body):

mac = hmac.new(github_secret, msg=body, digestmod=hashlib.sha256)
return hmac.compare_digest(mac.hexdigest(), signature)


def get_config():
with open("config.json", "r") as f:
config_data = json.loads(f.read())
return config_data
2 changes: 1 addition & 1 deletion api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
process_pr_desc,
)
from api.github_helper.utils import is_github_signature_valid
from api.github_helper.constants import CONFIG_DATA
from cloudcode.utils.config import CONFIG_DATA
import logging

logging.basicConfig(
Expand Down
17 changes: 13 additions & 4 deletions cloudcode/actions/reviews.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from cloudcode.helpers import output, parser
from typing import Optional
from cloudcode.llms.provider import LLMProvider
from cloudcode.llms.prompts import (
CODE_REVIEW_PROMPT,
Expand All @@ -14,23 +15,31 @@ def __init__(self):
self.provider = LLMProvider(system_prompt=CODE_REVIEW_SYSTEM_PROMPT)

def review_pull_request(
self, diff_text: str, pull_request_title: str, pull_request_desc: str
self,
diff_text: str,
pull_request_title: str,
pull_request_desc: str,
user: Optional[str] = None,
):
prompt = CODE_REVIEW_PROMPT.format(
PULL_REQUEST_TITLE=pull_request_title,
PULL_REQUEST_DESC=pull_request_desc,
CODE_DIFF=diff_text,
)

resp = self.provider.chat_completion(prompt)
resp = self.provider.chat_completion(prompt, user=user)

body = output.create_pr_review_from_json(parser.extract_json(resp))

# Share the review on pull request
return body

def generate_pull_request_desc(
self, diff_text: str, pull_request_title: str, pull_request_desc: str
self,
diff_text: str,
pull_request_title: str,
pull_request_desc: str,
user: Optional[str] = None,
):
"""
This method generates a AI powered description for a pull request.
Expand All @@ -41,7 +50,7 @@ def generate_pull_request_desc(
CODE_DIFF=diff_text,
)

resp = self.provider.chat_completion(prompt)
resp = self.provider.chat_completion(prompt, user=user)
self.logger.debug(f"PROMPT Generate PR Desc RESP: {resp}")
body = output.create_pr_description(
parser.extract_json(resp), pull_request_desc
Expand Down
14 changes: 11 additions & 3 deletions cloudcode/llms/provider.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from litellm import completion
import litellm
from cloudcode.llms.prompts import BASIC_SYSTEM_PROMPT
from cloudcode.utils.config import CONFIG_DATA


class LLMProvider:
Expand All @@ -18,16 +19,23 @@ def __init__(
self.model = model
self.max_tokens = max_tokens
self.temperature = temperature
if CONFIG_DATA.get("language_model", {}).get(
"enable_observability_logging", False
):
# set callbacks
litellm.success_callback = ["supabase"]
litellm.failure_callback = ["supabase"]

def chat_completion(self, prompt):
def chat_completion(self, prompt, user: str = None):
messages = [
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": prompt},
]
response = completion(
response = litellm.completion(
model=self.model,
messages=messages,
max_tokens=self.max_tokens,
temperature=self.temperature,
user=user,
)
return response["choices"][0]["message"]["content"]
32 changes: 32 additions & 0 deletions cloudcode/utils/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import json
import os


def get_config():
with open("config.json", "r") as f:
config_data = json.loads(f.read())
return config_data


def validate_config_settings(config):
"Make sure relvant enviorment variables are set"
if config.get("github_app", {}).get("check_signature", False):
if not os.environ.get("GITHUB_APP_WEBHOOK_SECRET"):
raise EnvironmentError(
"The environment variable 'GITHUB_APP_WEBHOOK_SECRET' is not set."
)

if config.get("language_model", {}).get("provider", {}) == "litellm":
if config.get("language_model", {}).get("enable_observability_logging", False):
if not os.environ.get("SUPABASE_URL"):
raise EnvironmentError(
"The environment variable 'SUPABASE_URL' is not set."
)
if not os.environ.get("SUPABASE_KEY"):
raise EnvironmentError(
"The environment variable 'SUPABASE_KEY' is not set."
)
return config


CONFIG_DATA = validate_config_settings(get_config())
Empty file removed cloudcode/utils/logging_config.py
Empty file.
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"language_model": {
"provider": "litellm"
"provider": "litellm",
"enable_observability_logging": true
},
"github_app": {
"check_signature": false
Expand Down
1 change: 1 addition & 0 deletions tests/actions/test_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def test_review_pull_request(valid_review):
valid_review["input"]["diff"],
valid_review["input"]["title"],
valid_review["input"]["description"],
user="pytest",
)

assert fuzz.ratio(result, valid_review["output"]) > 95
Loading