Skip to content

Commit

Permalink
fix: source code (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
xingwanying authored Aug 22, 2024
2 parents 45f631d + 19bc5c3 commit 404c041
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions server/tools/sourcecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
from github import Github
from github.ContentFile import ContentFile
from langchain.tools import tool
import json

DEFAULT_REPO_NAME = "ant-design/ant-design"

g = Github()


@tool
def search_code(
keyword: str,
repo_name: Optional[str] = DEFAULT_REPO_NAME,
max_num: Optional[int] = 5,
keyword: str,
repo_name: Optional[str] = DEFAULT_REPO_NAME,
max_num: Optional[int] = 5,
) -> List[ContentFile]:
"""
Searches for code files on GitHub that contain the given keyword.
Expand All @@ -23,13 +25,19 @@ def search_code(
:return: A list of ContentFile objects representing the matching code files.
"""
try:
query = f'repo:{repo_name} {keyword}'
query = f"repo:{repo_name} {keyword}"

# Perform the search for code files containing the keyword
code_files = g.search_code(query=query)[:max_num]

return code_files
code_list = [
{
"content": file.content,
"html_url": file.html_url,
"text_matches": file.text_matches,
}
for file in code_files
]
return json.dumps(code_list)
except Exception as e:
print(f"An error occurred: {e}")
return None

0 comments on commit 404c041

Please sign in to comment.