-
Notifications
You must be signed in to change notification settings - Fork 33
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(llm): support async/streaming output mode in api layer #179
Open
chiruu12
wants to merge
15
commits into
apache:main
Choose a base branch
from
chiruu12:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1e39f04
Adding support for async and streaming output mode
chiruu12 23e4680
Merge branch 'main' into main
imbajin 8c7dbaf
Update and rename hugegraph-python-client.yml to python-client.yml
imbajin 8c9f0f8
Adding support for async and streaming output mode
chiruu12 66c221e
Adding support for async and streaming output mode
chiruu12 fb233f8
Merge remote-tracking branch 'origin/main'
chiruu12 210b4e2
Adding support for async and streaming output mode
chiruu12 06ef02d
Adding support for async and streaming output mode
chiruu12 6dd0b7f
Adding support for async and streaming output mode
chiruu12 1d70b2a
Adding support for async and streaming output mode
chiruu12 05e7452
Adding support for async and streaming output mode
chiruu12 dfde6b0
Merge branch 'main' into main
imbajin d4cd537
fix admin_api.py usage error
imbajin d37b52e
tiny improve
imbajin ebdc387
Merge branch 'main' into main
imbajin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from fastapi import status, APIRouter | ||
|
||
from hugegraph_llm.api.exceptions.rag_exceptions import generate_response | ||
from hugegraph_llm.api.models.rag_requests import ( | ||
GraphConfigRequest, | ||
LLMConfigRequest, | ||
RerankerConfigRequest, | ||
) | ||
from hugegraph_llm.api.models.rag_response import RAGResponse | ||
from hugegraph_llm.config import llm_settings | ||
|
||
|
||
async def config_http_api( | ||
router: APIRouter, | ||
apply_graph_conf, | ||
apply_llm_conf, | ||
apply_embedding_conf, | ||
apply_reranker_conf, | ||
): | ||
@router.post("/config/graph", status_code=status.HTTP_201_CREATED) | ||
async def graph_config_api(req: GraphConfigRequest): | ||
res = await apply_graph_conf(req.ip, req.port, req.name, | ||
req.user, req.pwd, req.gs, origin_call="http") | ||
return generate_response(RAGResponse(status_code=res, message="Missing Value")) | ||
|
||
@router.post("/config/llm", status_code=status.HTTP_201_CREATED) | ||
async def llm_config_api(req: LLMConfigRequest): | ||
llm_settings.llm_type = req.llm_type | ||
|
||
if req.llm_type == "openai": | ||
res = await apply_llm_conf(req.api_key, req.api_base, req.language_model, | ||
req.max_tokens, origin_call="http") | ||
elif req.llm_type == "qianfan_wenxin": | ||
res = await apply_llm_conf(req.api_key, req.secret_key, req.language_model, | ||
None, origin_call="http") | ||
else: | ||
res = await apply_llm_conf(req.host, req.port, req.language_model, | ||
None, origin_call="http") | ||
return generate_response(RAGResponse(status_code=res, message="Missing Value")) | ||
|
||
@router.post("/config/embedding", status_code=status.HTTP_201_CREATED) | ||
async def embedding_config_api(req: LLMConfigRequest): | ||
llm_settings.embedding_type = req.llm_type | ||
|
||
if req.llm_type == "openai": | ||
res = await apply_embedding_conf(req.api_key, req.api_base, | ||
req.language_model, origin_call="http") | ||
elif req.llm_type == "qianfan_wenxin": | ||
res = await apply_embedding_conf(req.api_key, req.api_base, | ||
None, origin_call="http") | ||
else: | ||
res = await apply_embedding_conf(req.host, req.port, req.language_model, | ||
origin_call="http") | ||
return generate_response(RAGResponse(status_code=res, message="Missing Value")) | ||
|
||
@router.post("/config/rerank", status_code=status.HTTP_201_CREATED) | ||
async def rerank_config_api(req: RerankerConfigRequest): | ||
llm_settings.reranker_type = req.reranker_type | ||
|
||
if req.reranker_type == "cohere": | ||
res = await apply_reranker_conf(req.api_key, req.reranker_model, | ||
req.cohere_base_url, origin_call="http") | ||
elif req.reranker_type == "siliconflow": | ||
res = await apply_reranker_conf(req.api_key, req.reranker_model, | ||
None, origin_call="http") | ||
else: | ||
res = status.HTTP_501_NOT_IMPLEMENTED | ||
return generate_response(RAGResponse(status_code=res, message="Missing Value")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,35 +15,27 @@ | |
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
|
||
import json | ||
|
||
from fastapi import status, APIRouter, HTTPException | ||
|
||
from hugegraph_llm.api.exceptions.rag_exceptions import generate_response | ||
from fastapi import status, APIRouter, HTTPException | ||
from hugegraph_llm.api.models.rag_requests import ( | ||
RAGRequest, | ||
GraphConfigRequest, | ||
LLMConfigRequest, | ||
RerankerConfigRequest, | ||
GraphRAGRequest, | ||
) | ||
from hugegraph_llm.api.models.rag_response import RAGResponse | ||
from hugegraph_llm.config import llm_settings, prompt | ||
from hugegraph_llm.config import prompt | ||
from hugegraph_llm.utils.log import log | ||
|
||
|
||
def rag_http_api( | ||
router: APIRouter, | ||
rag_answer_func, | ||
graph_rag_recall_func, | ||
apply_graph_conf, | ||
apply_llm_conf, | ||
apply_embedding_conf, | ||
apply_reranker_conf, | ||
async def rag_http_api( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will try to resolve the issues and then I will ping you again sir! |
||
router: APIRouter, | ||
rag_answer_func, | ||
graph_rag_recall_func, | ||
): | ||
@router.post("/rag", status_code=status.HTTP_200_OK) | ||
def rag_answer_api(req: RAGRequest): | ||
result = rag_answer_func( | ||
async def rag_answer_api(req: RAGRequest): | ||
result = await rag_answer_func( | ||
text=req.query, | ||
raw_answer=req.raw_answer, | ||
vector_only_answer=req.vector_only, | ||
|
@@ -54,24 +46,26 @@ def rag_answer_api(req: RAGRequest): | |
near_neighbor_first=req.near_neighbor_first, | ||
custom_related_information=req.custom_priority_info, | ||
answer_prompt=req.answer_prompt or prompt.answer_prompt, | ||
keywords_extract_prompt=req.keywords_extract_prompt or prompt.keywords_extract_prompt, | ||
keywords_extract_prompt=req.keywords_extract_prompt | ||
or prompt.keywords_extract_prompt, | ||
gremlin_tmpl_num=req.gremlin_tmpl_num, | ||
gremlin_prompt=req.gremlin_prompt or prompt.gremlin_generate_prompt, | ||
) | ||
# TODO: we need more info in the response for users to understand the query logic | ||
|
||
return { | ||
"query": req.query, | ||
**{ | ||
key: value | ||
for key, value in zip(["raw_answer", "vector_only", "graph_only", "graph_vector_answer"], result) | ||
for key, value in zip(["raw_answer", "vector_only", "graph_only", | ||
"graph_vector_answer"], result) | ||
if getattr(req, key) | ||
}, | ||
} | ||
|
||
@router.post("/rag/graph", status_code=status.HTTP_200_OK) | ||
def graph_rag_recall_api(req: GraphRAGRequest): | ||
async def graph_rag_recall_api(req: GraphRAGRequest): | ||
try: | ||
result = graph_rag_recall_func( | ||
result = await graph_rag_recall_func( | ||
query=req.query, | ||
gremlin_tmpl_num=req.gremlin_tmpl_num, | ||
rerank_method=req.rerank_method, | ||
|
@@ -92,7 +86,7 @@ def graph_rag_recall_api(req: GraphRAGRequest): | |
] | ||
user_result = {key: result[key] for key in params if key in result} | ||
return {"graph_recall": user_result} | ||
# Note: Maybe only for qianfan/wenxin | ||
|
||
return {"graph_recall": json.dumps(result)} | ||
|
||
except TypeError as e: | ||
|
@@ -101,48 +95,6 @@ def graph_rag_recall_api(req: GraphRAGRequest): | |
except Exception as e: | ||
log.error("Unexpected error occurred: %s", e) | ||
raise HTTPException( | ||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="An unexpected error occurred." | ||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, | ||
detail="An unexpected error occurred." | ||
) from e | ||
|
||
@router.post("/config/graph", status_code=status.HTTP_201_CREATED) | ||
def graph_config_api(req: GraphConfigRequest): | ||
# Accept status code | ||
res = apply_graph_conf(req.ip, req.port, req.name, req.user, req.pwd, req.gs, origin_call="http") | ||
return generate_response(RAGResponse(status_code=res, message="Missing Value")) | ||
|
||
# TODO: restructure the implement of llm to three types, like "/config/chat_llm" | ||
@router.post("/config/llm", status_code=status.HTTP_201_CREATED) | ||
def llm_config_api(req: LLMConfigRequest): | ||
llm_settings.llm_type = req.llm_type | ||
|
||
if req.llm_type == "openai": | ||
res = apply_llm_conf(req.api_key, req.api_base, req.language_model, req.max_tokens, origin_call="http") | ||
elif req.llm_type == "qianfan_wenxin": | ||
res = apply_llm_conf(req.api_key, req.secret_key, req.language_model, None, origin_call="http") | ||
else: | ||
res = apply_llm_conf(req.host, req.port, req.language_model, None, origin_call="http") | ||
return generate_response(RAGResponse(status_code=res, message="Missing Value")) | ||
|
||
@router.post("/config/embedding", status_code=status.HTTP_201_CREATED) | ||
def embedding_config_api(req: LLMConfigRequest): | ||
llm_settings.embedding_type = req.llm_type | ||
|
||
if req.llm_type == "openai": | ||
res = apply_embedding_conf(req.api_key, req.api_base, req.language_model, origin_call="http") | ||
elif req.llm_type == "qianfan_wenxin": | ||
res = apply_embedding_conf(req.api_key, req.api_base, None, origin_call="http") | ||
else: | ||
res = apply_embedding_conf(req.host, req.port, req.language_model, origin_call="http") | ||
return generate_response(RAGResponse(status_code=res, message="Missing Value")) | ||
|
||
@router.post("/config/rerank", status_code=status.HTTP_201_CREATED) | ||
def rerank_config_api(req: RerankerConfigRequest): | ||
llm_settings.reranker_type = req.reranker_type | ||
|
||
if req.reranker_type == "cohere": | ||
res = apply_reranker_conf(req.api_key, req.reranker_model, req.cohere_base_url, origin_call="http") | ||
elif req.reranker_type == "siliconflow": | ||
res = apply_reranker_conf(req.api_key, req.reranker_model, None, origin_call="http") | ||
else: | ||
res = status.HTTP_501_NOT_IMPLEMENTED | ||
return generate_response(RAGResponse(status_code=res, message="Missing Value")) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, could tell me how u test the APIs? By directly request them?
The gradio UI loss the API link now
Before:

Now:

Maybe refer here: (Or Gradio's mount doc?)
incubator-hugegraph-ai/hugegraph-llm/src/hugegraph_llm/demo/rag_demo/app.py
Line 182 in 7ae5d6f