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(llm): support basic rag api(v1) #64

Merged
merged 33 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
84ac54b
feat(llm): integrate fastapi
ChenZiHong-Gavin Aug 13, 2024
c67b43e
feat: graph rag api demo
ChenZiHong-Gavin Aug 13, 2024
5712554
feat(rag_web_demo): Synchronize hg-ai branch "graphspace"
returnToInnocence Aug 14, 2024
1d0cffe
feat(config): Add configuration for graph_space.
returnToInnocence Aug 14, 2024
38a4569
feat(rag_web_demo): graphConfig interface encapsulates and tests.
returnToInnocence Aug 14, 2024
5dc6772
feat(rag_web_demo): Encapsulate llm config, split llm config function
returnToInnocence Aug 15, 2024
fa6df7b
feat(rag_web_demo): Add the Set up the LLM http interface
returnToInnocence Aug 15, 2024
f2d1c43
fix(rag_web_demo): Fix possible errors caused by Unsupported method i…
returnToInnocence Aug 15, 2024
3235e07
feat(rag_web_demo): The embedding_config_api interface completes the …
returnToInnocence Aug 15, 2024
e1943b8
fix(rag_web_demo): Fix the setting missing in apply_embedding_configu…
returnToInnocence Aug 15, 2024
d6e6e20
refactor(rag_web_demo): Split the code inside the main function based…
returnToInnocence Aug 15, 2024
4ae6b85
fix(rag_web_demo): Change the RAGRequest parameter defaults
returnToInnocence Aug 15, 2024
5be4cf2
fix(config): graph_space option change to None
returnToInnocence Aug 15, 2024
e33762a
fix(rag_web_demo): Changing code comments
returnToInnocence Aug 15, 2024
e8a22d3
style(rag_web_demo): Add a space after the colon
returnToInnocence Aug 16, 2024
7631a5f
fix(rag_web_demo): Fixed missing return value of apply_embedding_conf…
returnToInnocence Aug 16, 2024
ddc7931
tiny fix
imbajin Aug 16, 2024
967e950
fix(rag_web_demo): Fixed test_api_connection gr.Error unable to rende…
returnToInnocence Aug 16, 2024
5a0dff6
Merge branch 'main' into fast-api
imbajin Aug 17, 2024
07e552d
clean the func & tiny fix
imbajin Aug 17, 2024
aed8986
Merge branch 'fast-api' of https://github.com/ChenZiHong-Gavin/incuba…
returnToInnocence Aug 17, 2024
93aa516
refactor(rag_api): refactor return && some tiny thing
returnToInnocence Aug 17, 2024
f2c12e6
fix(rag_web_demo): unify build_kg func return type
returnToInnocence Aug 17, 2024
b7ea009
feat:add try catch for test connection
ChenZiHong-Gavin Aug 17, 2024
ccbfa33
feat: use constants for build_mode
ChenZiHong-Gavin Aug 17, 2024
83950b9
style(rag_web_demo,rag_api): use code_format_and_analysis.sh format code
returnToInnocence Aug 17, 2024
ec2f8e0
fix: compare constant values
ChenZiHong-Gavin Aug 18, 2024
c2f91bc
feat(rag_api): Split wrapper api
returnToInnocence Aug 18, 2024
245583b
fix(rag_web_demo): fix test_api_connection
returnToInnocence Aug 18, 2024
6cb562b
Update rag_web_demo.py
imbajin Aug 18, 2024
a3652a7
Merge branch 'fast-api' of https://github.com/ChenZiHong-Gavin/incuba…
imbajin Aug 18, 2024
aa47e92
refact: support test wenxin/ollama conn
imbajin Aug 18, 2024
e097d0f
tiny fix
imbajin Aug 18, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,4 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
*.DS_Store
16 changes: 16 additions & 0 deletions hugegraph-llm/src/hugegraph_llm/api/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.
37 changes: 37 additions & 0 deletions hugegraph-llm/src/hugegraph_llm/api/exceptions/rag_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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 HTTPException
from hugegraph_llm.api.models.rag_response import RAGResponse


class ExternalException(HTTPException):
def __init__(self):
super().__init__(status_code=400, detail="Connect failed with error code -1, please check the input.")


class ConnectionFailedException(HTTPException):
def __init__(self, status_code: int, message: str):
super().__init__(status_code=status_code, detail=message)


def generate_response(response: RAGResponse) -> dict:
if response.status_code == -1:
raise ExternalException()
elif not (200 <= response.status_code < 300):
raise ConnectionFailedException(response.status_code, response.message)
return {"message": "Connection successful. Configured finished."}
16 changes: 16 additions & 0 deletions hugegraph-llm/src/hugegraph_llm/api/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.
52 changes: 52 additions & 0 deletions hugegraph-llm/src/hugegraph_llm/api/models/rag_requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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 pydantic import BaseModel
from typing import Optional


class RAGRequest(BaseModel):
query: str
raw_llm: Optional[bool] = True
vector_only: Optional[bool] = False
graph_only: Optional[bool] = False
graph_vector: Optional[bool] = False


class GraphConfigRequest(BaseModel):
ip: str = "127.0.0.1"
port: str = "8080"
name: str = "hugegraph"
user: str = "xxx"
pwd: str = "xxx"
gs: str = None


class LLMConfigRequest(BaseModel):
llm_type: str
# The common parameters shared by OpenAI, Qianfan Wenxin,
# and OLLAMA platforms.
api_key: str
api_base: str
language_model: str
# Openai-only properties
max_tokens: str = None
# qianfan-wenxin-only properties
secret_key: str = None
# ollama-only properties
host: str = None
port: str = None
23 changes: 23 additions & 0 deletions hugegraph-llm/src/hugegraph_llm/api/models/rag_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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 pydantic import BaseModel


class RAGResponse(BaseModel):
status_code: int = -1
message: str = ""
50 changes: 50 additions & 0 deletions hugegraph-llm/src/hugegraph_llm/api/rag_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,53 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from fastapi import FastAPI, status

from hugegraph_llm.api.models.rag_response import RAGResponse
from hugegraph_llm.config import settings
from hugegraph_llm.api.models.rag_requests import RAGRequest, GraphConfigRequest, LLMConfigRequest
from hugegraph_llm.api.exceptions.rag_exceptions import generate_response


def rag_http_api(app: FastAPI, rag_answer_func, apply_graph_conf, apply_llm_conf, apply_embedding_conf):
@app.post("/rag", status_code=status.HTTP_200_OK)
def rag_answer_api(req: RAGRequest):
result = rag_answer_func(req.query, req.raw_llm, req.vector_only, req.graph_only, req.graph_vector)
return {
key: value
for key, value in zip(["raw_llm", "vector_only", "graph_only", "graph_vector"], result)
if getattr(req, key)
}

@app.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"))

@app.post("/config/llm", status_code=status.HTTP_201_CREATED)
def llm_config_api(req: LLMConfigRequest):
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"))

@app.post("/config/embedding", status_code=status.HTTP_201_CREATED)
def embedding_config_api(req: LLMConfigRequest):
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"))
2 changes: 1 addition & 1 deletion hugegraph-llm/src/hugegraph_llm/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Config:
"""HugeGraph settings"""
graph_ip: Optional[str] = "127.0.0.1"
graph_port: Optional[int] = 8080
# graph_space: Optional[str] = "DEFAULT"
graph_space: Optional[str] = None
graph_name: Optional[str] = "hugegraph"
graph_user: Optional[str] = "admin"
graph_pwd: Optional[str] = "xxx"
Expand Down
Loading
Loading