Skip to content

Commit

Permalink
feat(llm): support basic rag api(v1) (#64)
Browse files Browse the repository at this point in the history
* refact: support test wenxin/ollama conn

---------

Co-authored-by: Hongjun Li <[email protected]>
Co-authored-by: imbajin <[email protected]>
  • Loading branch information
3 people authored Aug 18, 2024
1 parent 0a9bf24 commit 4f6414d
Show file tree
Hide file tree
Showing 10 changed files with 430 additions and 177 deletions.
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

0 comments on commit 4f6414d

Please sign in to comment.