-
Notifications
You must be signed in to change notification settings - Fork 60
feat(llm): support auto llm review and fix test_api_connection #222
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
Open
MrJs133
wants to merge
8
commits into
apache:main
Choose a base branch
from
MrJs133:auto_test_llms
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
310f7ff
support auto llm review and fix test_api_connection
ab7d987
pylint
4258523
Merge branch 'main' into auto_test_llms
imbajin 4020f69
Merge branch 'main' into auto_test_llms
imbajin 93620ec
output time
87c20be
pylint
addc2d8
Merge branch 'main' into auto_test_llms
imbajin 34fc33f
try except
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 hidden or 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 hidden or 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 hidden or 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
11 changes: 11 additions & 0 deletions
11
hugegraph-llm/src/hugegraph_llm/resources/demo/llm_review.yaml
This file contains hidden or 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,11 @@ | ||
- type: openai | ||
model_name: ernie-4.5-8k-preview | ||
api_key: | ||
api_base: | ||
max_tokens: 2048 | ||
|
||
- type: openai | ||
model_name: gpt-4.1-mini | ||
api_key: | ||
api_base: | ||
max_tokens: 4096 |
236 changes: 236 additions & 0 deletions
236
hugegraph-llm/src/hugegraph_llm/utils/other_tool_utils.py
This file contains hidden or 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,236 @@ | ||
# 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. | ||
|
||
import time | ||
import json | ||
import re | ||
import gradio as gr | ||
import yaml | ||
|
||
from hugegraph_llm.config import PromptConfig | ||
from hugegraph_llm.utils.log import log | ||
from hugegraph_llm.models.llms.ollama import OllamaClient | ||
from hugegraph_llm.models.llms.openai import OpenAIClient | ||
from hugegraph_llm.models.llms.qianfan import QianfanClient | ||
from hugegraph_llm.models.llms.litellm import LiteLLMClient | ||
def judge(answers, standard_answer, review_model_name, review_max_tokens, key, base): | ||
try: | ||
review_client = OpenAIClient( | ||
api_key=key, | ||
api_base=base, | ||
model_name=review_model_name, | ||
max_tokens=int(review_max_tokens) | ||
) | ||
review_prompt = PromptConfig.review_prompt.format(standard_answer=standard_answer) | ||
for _, (model_name, answer) in enumerate(answers.items(), start=1): | ||
review_prompt += f"### {model_name}:\n{answer.strip()}\n\n" | ||
log.debug("Review_prompt: %s", review_prompt) | ||
response = review_client.generate(prompt=review_prompt) | ||
log.debug("orig_review_response: %s", response) | ||
match = re.search(r'```json\n(.*?)\n```', response, re.DOTALL) | ||
if match: | ||
response = match.group(1).strip() | ||
reviews = json.loads(response) | ||
return reviews | ||
except Exception as e: # pylint: disable=W0718 | ||
log.error("Review failed: %s", str(e)) | ||
reviews = {"error": f"Review error: {str(e)}"} | ||
return reviews | ||
|
||
def parse_llm_configurations(config_text: str): | ||
configs = [] | ||
lines = config_text.strip().split("\n") | ||
for i, line in enumerate(lines, 1): | ||
fields = [x.strip() for x in line.split(",")] | ||
if not fields: | ||
continue | ||
llm_type = fields[0] | ||
try: | ||
if llm_type == "openai": | ||
# openai, model_name, api_key, api_base, max_tokens | ||
model_name, api_key, api_base, max_tokens = fields[1:5] | ||
configs.append({ | ||
"type": "openai", | ||
"model_name": model_name, | ||
"api_key": api_key, | ||
"api_base": api_base, | ||
"max_tokens": int(max_tokens), | ||
}) | ||
elif llm_type == "qianfan_wenxin": | ||
# qianfan_wenxin, model_name, api_key, secret_key | ||
model_name, api_key, secret_key = fields[1:4] | ||
configs.append({ | ||
"type": "qianfan_wenxin", | ||
"model_name": model_name, | ||
"api_key": api_key, | ||
"secret_key": secret_key, | ||
}) | ||
elif llm_type == "ollama/local": | ||
# ollama/local, model_name, host, port, max_tokens | ||
model_name, host, port, max_tokens = fields[1:5] | ||
configs.append({ | ||
"type": "ollama/local", | ||
"model_name": model_name, | ||
"host": host, | ||
"port": int(port), | ||
"max_tokens": int(max_tokens), | ||
}) | ||
elif llm_type == "litellm": | ||
# litellm, model_name, api_key, api_base, max_tokens | ||
model_name, api_key, api_base, max_tokens = fields[1:5] | ||
configs.append({ | ||
"type": "litellm", | ||
"model_name": model_name, | ||
"api_key": api_key, | ||
"api_base": api_base, | ||
"max_tokens": int(max_tokens), | ||
}) | ||
else: | ||
raise ValueError(f"Unsupported llm type '{llm_type}' in line {i}") | ||
except Exception as e: | ||
raise ValueError(f"Error parsing line {i}: {line}\nDetails: {e}") from e | ||
return configs | ||
|
||
def parse_llm_configurations_from_yaml(yaml_file_path: str): | ||
configs = [] | ||
with open(yaml_file_path, "r", encoding="utf-8") as f: | ||
raw_configs = yaml.safe_load(f) | ||
if not isinstance(raw_configs, list): | ||
raise ValueError("YAML 文件内容必须是一个 LLM 配置列表。") | ||
for i, config in enumerate(raw_configs, 1): | ||
try: | ||
llm_type = config.get("type") | ||
if llm_type == "openai": | ||
configs.append({ | ||
"type": "openai", | ||
"model_name": config["model_name"], | ||
"api_key": config["api_key"], | ||
"api_base": config["api_base"], | ||
"max_tokens": int(config["max_tokens"]), | ||
}) | ||
elif llm_type == "qianfan_wenxin": | ||
configs.append({ | ||
"type": "qianfan_wenxin", | ||
"model_name": config["model_name"], | ||
"api_key": config["api_key"], | ||
"secret_key": config["secret_key"], | ||
}) | ||
elif llm_type == "ollama/local": | ||
configs.append({ | ||
"type": "ollama/local", | ||
"model_name": config["model_name"], | ||
"host": config["host"], | ||
"port": int(config["port"]), | ||
"max_tokens": int(config["max_tokens"]), | ||
}) | ||
elif llm_type == "litellm": | ||
configs.append({ | ||
"type": "litellm", | ||
"model_name": config["model_name"], | ||
"api_key": config["api_key"], | ||
"api_base": config["api_base"], | ||
"max_tokens": int(config["max_tokens"]), | ||
}) | ||
else: | ||
raise ValueError(f"不支持的 llm type '{llm_type}',在配置第 {i} 项") | ||
except Exception as e: | ||
raise ValueError(f"解析配置第 {i} 项失败: {e}") from e | ||
|
||
return configs | ||
|
||
|
||
def auto_test_llms( | ||
llm_configs, | ||
llm_configs_file, | ||
prompt, | ||
standard_answer, | ||
review_model_name, | ||
review_max_tokens, | ||
key, | ||
base, | ||
fmt=True | ||
): | ||
configs = None | ||
if llm_configs_file and llm_configs: | ||
raise gr.Error("Please only choose one between file and text.") | ||
if llm_configs: | ||
configs = parse_llm_configurations(llm_configs) | ||
elif llm_configs_file: | ||
configs = parse_llm_configurations_from_yaml(llm_configs_file) | ||
log.debug("LLM_configs: %s", configs) | ||
answers = {} | ||
for config in configs: | ||
output = None | ||
time_start = time.perf_counter() | ||
try: | ||
if config["type"] == "openai": | ||
client = OpenAIClient( | ||
api_key=config["api_key"], | ||
api_base=config["api_base"], | ||
model_name=config["model_name"], | ||
max_tokens=config["max_tokens"], | ||
) | ||
output = client.generate(prompt=prompt) | ||
elif config["type"] == "qianfan_wenxin": | ||
client = QianfanClient( | ||
model_name=config["model_name"], | ||
api_key=config["api_key"], | ||
secret_key=config["secret_key"] | ||
) | ||
output = client.generate(prompt=prompt) | ||
elif config["type"] == "ollama/local": | ||
client = OllamaClient( | ||
model_name=config["model_name"], | ||
host=config["host"], | ||
port=config["port"], | ||
) | ||
output = client.generate(prompt=prompt) | ||
elif config["type"] == "litellm": | ||
client = LiteLLMClient( | ||
api_key=config["api_key"], | ||
api_base=config["api_base"], | ||
model_name=config["model_name"], | ||
max_tokens=config["max_tokens"], | ||
) | ||
output = client.generate(prompt=prompt) | ||
except Exception as e: # pylint: disable=broad-except | ||
log.error("Generate failed for %s: %s", config["model_name"], e) | ||
output = f"[ERROR] {e}" | ||
time_end = time.perf_counter() | ||
latency = time_end - time_start | ||
answers[config["model_name"]] = { | ||
"answer": output, | ||
"latency": f"{round(latency, 2)}s" | ||
} | ||
reviews = judge( | ||
{k: v["answer"] for k, v in answers.items()}, | ||
standard_answer, | ||
review_model_name, | ||
review_max_tokens, | ||
key, | ||
base | ||
) | ||
log.debug("reviews: %s", reviews) | ||
result = {} | ||
reviews_dict = {item["model"]: item for item in reviews} if isinstance(reviews, list) else reviews | ||
for model_name, infos in answers.items(): | ||
result[model_name] = { | ||
"answer": infos["answer"], | ||
"latency": infos["latency"], | ||
"review": reviews_dict.get(model_name, {}) | ||
} | ||
return json.dumps(result, indent=4, ensure_ascii=False) if fmt else reviews |
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.
Uh oh!
There was an error while loading. Please reload this page.