Skip to content

Commit

Permalink
fix: fix qa_chat (#235)
Browse files Browse the repository at this point in the history
修复 qa_chat 遗漏的access_token
  • Loading branch information
RaoHai authored Aug 21, 2024
2 parents cf2d373 + 57cfb9e commit 19acb7a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
9 changes: 5 additions & 4 deletions server/agent/qa_chat.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import AsyncIterator
from typing import AsyncIterator, Optional
from agent.base import AgentBuilder
from prompts.bot_template import generate_prompt_by_repo_name
from petercat_utils import get_client
from petercat_utils.data_class import ChatData
from tools import issue, sourcecode, knowledge, git_info


def get_tools(bot_id, token):
def get_tools(bot_id: str, token: Optional[str]):
issue_tools = issue.factory(access_token=token)
return {
"search_knowledge": knowledge.factory(bot_id=bot_id),
Expand Down Expand Up @@ -49,8 +49,9 @@ def agent_stream_chat(input_data: ChatData, user_token: str) -> AsyncIterator[st
return agent.run_stream_chat(input_data)


def agent_chat(input_data: ChatData) -> AsyncIterator[str]:
def agent_chat(input_data: ChatData, user_token: Optional[str]) -> AsyncIterator[str]:
agent = AgentBuilder(
prompt=init_prompt(input_data), tools=get_tools(input_data.bot_id)
prompt=init_prompt(input_data),
tools=get_tools(input_data.bot_id, token=user_token),
)
return agent.run_chat(input_data)
2 changes: 1 addition & 1 deletion server/event_handler/discussion.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async def handle_discussion_event(self, action: str):
discussion_number = discussion["number"]
message = Message(role="user", content=[text_block])

analysis_result = await agent_chat(ChatData(messages=[message]))
analysis_result = await agent_chat(ChatData(messages=[message]), None)
discussion_id = await self.get_discussion_id(owner, repo, discussion_number)
await self.create_discussion_comment(discussion_id, analysis_result["output"])

Expand Down
33 changes: 15 additions & 18 deletions server/event_handler/issue.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

from typing import Any
from github import Github, Auth
from github import GithubException
from agent.qa_chat import agent_chat

from petercat_utils.data_class import ChatData, Message, TextContentBlock

class IssueEventHandler():

class IssueEventHandler:
event: Any
auth: Auth.AppAuth
g: Github
Expand All @@ -18,23 +18,20 @@ def __init__(self, payload: Any, auth: Auth.AppAuth) -> None:

async def execute(self):
try:
print('actions:', self.event['action'])
if self.event['action'] == "opened":
repo_name = self.event['repository']["full_name"]
issue_number = self.event["issue"]["number"]
repo = self.g.get_repo(repo_name)
issue = repo.get_issue(number=issue_number)
issue_content = f"{issue.title}: {issue.body}"
text_block = TextContentBlock(
type="text",
text=issue_content
)
issue_content = issue.body
message = Message(role="user", content=[text_block])
analysis_result = await agent_chat(ChatData(messages=[message]))
issue.create_comment(analysis_result['output'])
print("actions:", self.event["action"])
if self.event["action"] == "opened":
repo_name = self.event["repository"]["full_name"]
issue_number = self.event["issue"]["number"]
repo = self.g.get_repo(repo_name)
issue = repo.get_issue(number=issue_number)
issue_content = f"{issue.title}: {issue.body}"
text_block = TextContentBlock(type="text", text=issue_content)
issue_content = issue.body
message = Message(role="user", content=[text_block])
analysis_result = await agent_chat(ChatData(messages=[message]), None)
issue.create_comment(analysis_result["output"])

return {"success": True}
return {"success": True}
except GithubException as e:
print(f"处理 GitHub 请求时出错:{e}")
return {"success": False, "error": str(e)}
44 changes: 35 additions & 9 deletions server/routers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,52 @@
responses={404: {"description": "Not found"}},
)


async def generate_auth_failed_stream():
message = "Auth failed, please login first\n\n"
yield message

@router.post("/stream_qa", response_class=StreamingResponse, dependencies=[Depends(verify_rate_limit)])
def run_qa_chat(input_data: ChatData, user_access_token: Annotated[str | None, Depends(get_user_access_token)] = None):
print(f"run_qa_chat: input_data={input_data}, user_access_token={user_access_token}")
result = qa_chat.agent_stream_chat(input_data=input_data, user_token=user_access_token)

@router.post(
"/stream_qa",
response_class=StreamingResponse,
dependencies=[Depends(verify_rate_limit)],
)
def run_qa_chat(
input_data: ChatData,
user_access_token: Annotated[str | None, Depends(get_user_access_token)] = None,
):
print(
f"run_qa_chat: input_data={input_data}, user_access_token={user_access_token}"
)
result = qa_chat.agent_stream_chat(
input_data=input_data, user_token=user_access_token
)
return StreamingResponse(result, media_type="text/event-stream")


@router.post("/qa", dependencies=[Depends(verify_rate_limit)])
async def run_issue_helper(input_data: ChatData):
result = await qa_chat.agent_chat(input_data)
async def run_issue_helper(
input_data: ChatData,
user_access_token: Annotated[str | None, Depends(get_user_access_token)] = None,
):
result = await qa_chat.agent_chat(input_data, user_access_token)
return result


@router.post("/stream_builder", response_class=StreamingResponse, dependencies=[Depends(verify_rate_limit)])
def run_bot_builder(input_data: ChatData, bot_id: Optional[str] = None, user_id: Annotated[str | None, Depends(get_user_id)] = None):
@router.post(
"/stream_builder",
response_class=StreamingResponse,
dependencies=[Depends(verify_rate_limit)],
)
def run_bot_builder(
input_data: ChatData,
bot_id: Optional[str] = None,
user_id: Annotated[str | None, Depends(get_user_id)] = None,
):
if not user_id:
return StreamingResponse(generate_auth_failed_stream(), media_type="text/event-stream")
return StreamingResponse(
generate_auth_failed_stream(), media_type="text/event-stream"
)
result = bot_builder.agent_stream_chat(input_data, user_id, bot_id)
return StreamingResponse(result, media_type="text/event-stream")

0 comments on commit 19acb7a

Please sign in to comment.