Skip to content

Commit

Permalink
Merge pull request #20 from COSCUP/feat/optimize_reply
Browse files Browse the repository at this point in the history
Feat/optimize reply
  • Loading branch information
106303551 authored Jul 11, 2024
2 parents 9bd9ab3 + 8323940 commit f8996a9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=<LANGCHAIN_API_KEY>
MODEL_API=<the model api from Mattermost>
ACCESS_TOKEN=<Github access token>
MATTERMOST_WEBHOOK_URL=<webhook url>
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=<LANGCHAIN_API_KEY>
MODEL_API=<the model api from Mattermost>
ACCESS_TOKEN=<Github access token>
MATTERMOST_WEBHOOK_URL=<Mattermost webhook url>
```
> Note:
> 1. You can get the `LANGCHAIN_API_KEY` from [LangSmith](https://www.langchain.com/langsmith).
> 2. Get the `MODEL_API` from [Mattermost](https://chat.coscup.org/coscup/pl/hjez3dwmtjbk8du1rih9ne66wo).
> 3. How to get Github access token: [Github Docs](https://docs.github.com/en/authentication/
> 4. Get the `MATTERMOST_WEBHOOK_URL` from Mattermost incoming webhook page and copy the URL from [polyhistor-wait](https://chat.coscup.org/coscup/integrations/incoming_webhooks)
keeping-your-account-and-data-secure/managing-your-personal-access-tokens).


Expand Down
20 changes: 19 additions & 1 deletion src/routers/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import warnings

import httpx
from dotenv import load_dotenv
from fastapi import APIRouter, Form, status
from fastapi.responses import JSONResponse
Expand Down Expand Up @@ -84,10 +85,27 @@ async def askAPI(data: Query):
tags=["external"],
)
async def chatbot(text: str = Form(...)):
if os.getenv("MODE") == "dev":
print("收到問題囉~請稍等一下")
else:
await post_to_mattermost("收到問題囉~請稍等一下")

data = Query(query=text.lower())
contents = await askAPI(data)

ans = "Question: " + text + "\n\n" + "Answer: " + "\n" + contents
ans = "Question: " + text + "\n\n" + "Answer: " + "\n" + contents + "\n\n" + "如果有什麼問題可以直接留言給我們,如果覺得答案不符合需求,也歡迎直接在留言處提供文件並標注 @jefflu 或 @jimmy_hong 或 @irischen"

gc.collect()
return JSONResponse(content={"response_type": "in_channel", "text": ans})


async def post_to_mattermost(message: str):
webhook_url = os.getenv("MATTERMOST_WEBHOOK_URL")
if not webhook_url:
print("Mattermost webhook URL is not set")
return

payload = {"text": message}

async with httpx.AsyncClient() as client:
response = await client.post(webhook_url, json=payload)

0 comments on commit f8996a9

Please sign in to comment.