Merge branch 'fix/agent-external-knowledge-retrieval' into fix/external-knowledge-retrieval-issues
commit
ed4e029609
@ -0,0 +1,25 @@
|
|||||||
|
model: llama-guard-3-8b
|
||||||
|
label:
|
||||||
|
zh_Hans: Llama-Guard-3-8B
|
||||||
|
en_US: Llama-Guard-3-8B
|
||||||
|
model_type: llm
|
||||||
|
features:
|
||||||
|
- agent-thought
|
||||||
|
model_properties:
|
||||||
|
mode: chat
|
||||||
|
context_size: 8192
|
||||||
|
parameter_rules:
|
||||||
|
- name: temperature
|
||||||
|
use_template: temperature
|
||||||
|
- name: top_p
|
||||||
|
use_template: top_p
|
||||||
|
- name: max_tokens
|
||||||
|
use_template: max_tokens
|
||||||
|
default: 512
|
||||||
|
min: 1
|
||||||
|
max: 8192
|
||||||
|
pricing:
|
||||||
|
input: '0.20'
|
||||||
|
output: '0.20'
|
||||||
|
unit: '0.000001'
|
||||||
|
currency: USD
|
||||||
@ -0,0 +1 @@
|
|||||||
|
- gte-rerank
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
model: gte-rerank
|
||||||
|
model_type: rerank
|
||||||
|
model_properties:
|
||||||
|
context_size: 4000
|
||||||
@ -0,0 +1,136 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import dashscope
|
||||||
|
from dashscope.common.error import (
|
||||||
|
AuthenticationError,
|
||||||
|
InvalidParameter,
|
||||||
|
RequestFailure,
|
||||||
|
ServiceUnavailableError,
|
||||||
|
UnsupportedHTTPMethod,
|
||||||
|
UnsupportedModel,
|
||||||
|
)
|
||||||
|
|
||||||
|
from core.model_runtime.entities.rerank_entities import RerankDocument, RerankResult
|
||||||
|
from core.model_runtime.errors.invoke import (
|
||||||
|
InvokeAuthorizationError,
|
||||||
|
InvokeBadRequestError,
|
||||||
|
InvokeConnectionError,
|
||||||
|
InvokeError,
|
||||||
|
InvokeRateLimitError,
|
||||||
|
InvokeServerUnavailableError,
|
||||||
|
)
|
||||||
|
from core.model_runtime.errors.validate import CredentialsValidateFailedError
|
||||||
|
from core.model_runtime.model_providers.__base.rerank_model import RerankModel
|
||||||
|
|
||||||
|
|
||||||
|
class GTERerankModel(RerankModel):
|
||||||
|
"""
|
||||||
|
Model class for GTE rerank model.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def _invoke(
|
||||||
|
self,
|
||||||
|
model: str,
|
||||||
|
credentials: dict,
|
||||||
|
query: str,
|
||||||
|
docs: list[str],
|
||||||
|
score_threshold: Optional[float] = None,
|
||||||
|
top_n: Optional[int] = None,
|
||||||
|
user: Optional[str] = None,
|
||||||
|
) -> RerankResult:
|
||||||
|
"""
|
||||||
|
Invoke rerank model
|
||||||
|
|
||||||
|
:param model: model name
|
||||||
|
:param credentials: model credentials
|
||||||
|
:param query: search query
|
||||||
|
:param docs: docs for reranking
|
||||||
|
:param score_threshold: score threshold
|
||||||
|
:param top_n: top n
|
||||||
|
:param user: unique user id
|
||||||
|
:return: rerank result
|
||||||
|
"""
|
||||||
|
if len(docs) == 0:
|
||||||
|
return RerankResult(model=model, docs=docs)
|
||||||
|
|
||||||
|
# initialize client
|
||||||
|
dashscope.api_key = credentials["dashscope_api_key"]
|
||||||
|
|
||||||
|
response = dashscope.TextReRank.call(
|
||||||
|
query=query,
|
||||||
|
documents=docs,
|
||||||
|
model=model,
|
||||||
|
top_n=top_n,
|
||||||
|
return_documents=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
rerank_documents = []
|
||||||
|
for _, result in enumerate(response.output.results):
|
||||||
|
# format document
|
||||||
|
rerank_document = RerankDocument(
|
||||||
|
index=result.index,
|
||||||
|
score=result.relevance_score,
|
||||||
|
text=result["document"]["text"],
|
||||||
|
)
|
||||||
|
|
||||||
|
# score threshold check
|
||||||
|
if score_threshold is not None:
|
||||||
|
if result.relevance_score >= score_threshold:
|
||||||
|
rerank_documents.append(rerank_document)
|
||||||
|
else:
|
||||||
|
rerank_documents.append(rerank_document)
|
||||||
|
|
||||||
|
return RerankResult(model=model, docs=rerank_documents)
|
||||||
|
|
||||||
|
def validate_credentials(self, model: str, credentials: dict) -> None:
|
||||||
|
"""
|
||||||
|
Validate model credentials
|
||||||
|
|
||||||
|
:param model: model name
|
||||||
|
:param credentials: model credentials
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
self.invoke(
|
||||||
|
model=model,
|
||||||
|
credentials=credentials,
|
||||||
|
query="What is the capital of the United States?",
|
||||||
|
docs=[
|
||||||
|
"Carson City is the capital city of the American state of Nevada. At the 2010 United States "
|
||||||
|
"Census, Carson City had a population of 55,274.",
|
||||||
|
"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean that "
|
||||||
|
"are a political division controlled by the United States. Its capital is Saipan.",
|
||||||
|
],
|
||||||
|
score_threshold=0.8,
|
||||||
|
)
|
||||||
|
except Exception as ex:
|
||||||
|
print(ex)
|
||||||
|
raise CredentialsValidateFailedError(str(ex))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _invoke_error_mapping(self) -> dict[type[InvokeError], list[type[Exception]]]:
|
||||||
|
"""
|
||||||
|
Map model invoke error to unified error
|
||||||
|
The key is the error type thrown to the caller
|
||||||
|
The value is the error type thrown by the model,
|
||||||
|
which needs to be converted into a unified error type for the caller.
|
||||||
|
|
||||||
|
:return: Invoke error mapping
|
||||||
|
"""
|
||||||
|
return {
|
||||||
|
InvokeConnectionError: [
|
||||||
|
RequestFailure,
|
||||||
|
],
|
||||||
|
InvokeServerUnavailableError: [
|
||||||
|
ServiceUnavailableError,
|
||||||
|
],
|
||||||
|
InvokeRateLimitError: [],
|
||||||
|
InvokeAuthorizationError: [
|
||||||
|
AuthenticationError,
|
||||||
|
],
|
||||||
|
InvokeBadRequestError: [
|
||||||
|
InvalidParameter,
|
||||||
|
UnsupportedModel,
|
||||||
|
UnsupportedHTTPMethod,
|
||||||
|
],
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,44 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
from typing import Any, Union
|
||||||
|
|
||||||
|
import pytz
|
||||||
|
|
||||||
|
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||||
|
from core.tools.errors import ToolInvokeError
|
||||||
|
from core.tools.tool.builtin_tool import BuiltinTool
|
||||||
|
|
||||||
|
|
||||||
|
class LocaltimeToTimestampTool(BuiltinTool):
|
||||||
|
def _invoke(
|
||||||
|
self,
|
||||||
|
user_id: str,
|
||||||
|
tool_parameters: dict[str, Any],
|
||||||
|
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||||
|
"""
|
||||||
|
Convert localtime to timestamp
|
||||||
|
"""
|
||||||
|
localtime = tool_parameters.get("localtime")
|
||||||
|
timezone = tool_parameters.get("timezone", "Asia/Shanghai")
|
||||||
|
if not timezone:
|
||||||
|
timezone = None
|
||||||
|
time_format = "%Y-%m-%d %H:%M:%S"
|
||||||
|
|
||||||
|
timestamp = self.localtime_to_timestamp(localtime, time_format, timezone)
|
||||||
|
if not timestamp:
|
||||||
|
return self.create_text_message(f"Invalid localtime: {localtime}")
|
||||||
|
|
||||||
|
return self.create_text_message(f"{timestamp}")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def localtime_to_timestamp(localtime: str, time_format: str, local_tz=None) -> int | None:
|
||||||
|
try:
|
||||||
|
if local_tz is None:
|
||||||
|
local_tz = datetime.now().astimezone().tzinfo
|
||||||
|
if isinstance(local_tz, str):
|
||||||
|
local_tz = pytz.timezone(local_tz)
|
||||||
|
local_time = datetime.strptime(localtime, time_format)
|
||||||
|
localtime = local_tz.localize(local_time)
|
||||||
|
timestamp = int(localtime.timestamp())
|
||||||
|
return timestamp
|
||||||
|
except Exception as e:
|
||||||
|
raise ToolInvokeError(str(e))
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
identity:
|
||||||
|
name: localtime_to_timestamp
|
||||||
|
author: zhuhao
|
||||||
|
label:
|
||||||
|
en_US: localtime to timestamp
|
||||||
|
zh_Hans: 获取时间戳
|
||||||
|
description:
|
||||||
|
human:
|
||||||
|
en_US: A tool for localtime convert to timestamp
|
||||||
|
zh_Hans: 获取时间戳
|
||||||
|
llm: A tool for localtime convert to timestamp
|
||||||
|
parameters:
|
||||||
|
- name: localtime
|
||||||
|
type: string
|
||||||
|
required: true
|
||||||
|
form: llm
|
||||||
|
label:
|
||||||
|
en_US: localtime
|
||||||
|
zh_Hans: 本地时间
|
||||||
|
human_description:
|
||||||
|
en_US: localtime, such as 2024-1-1 0:0:0
|
||||||
|
zh_Hans: 本地时间, 比如2024-1-1 0:0:0
|
||||||
|
- name: timezone
|
||||||
|
type: string
|
||||||
|
required: false
|
||||||
|
form: llm
|
||||||
|
label:
|
||||||
|
en_US: Timezone
|
||||||
|
zh_Hans: 时区
|
||||||
|
human_description:
|
||||||
|
en_US: Timezone, such as Asia/Shanghai
|
||||||
|
zh_Hans: 时区, 比如Asia/Shanghai
|
||||||
|
default: Asia/Shanghai
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
from typing import Any, Union
|
||||||
|
|
||||||
|
import pytz
|
||||||
|
|
||||||
|
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||||
|
from core.tools.errors import ToolInvokeError
|
||||||
|
from core.tools.tool.builtin_tool import BuiltinTool
|
||||||
|
|
||||||
|
|
||||||
|
class TimestampToLocaltimeTool(BuiltinTool):
|
||||||
|
def _invoke(
|
||||||
|
self,
|
||||||
|
user_id: str,
|
||||||
|
tool_parameters: dict[str, Any],
|
||||||
|
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||||
|
"""
|
||||||
|
Convert timestamp to localtime
|
||||||
|
"""
|
||||||
|
timestamp = tool_parameters.get("timestamp")
|
||||||
|
timezone = tool_parameters.get("timezone", "Asia/Shanghai")
|
||||||
|
if not timezone:
|
||||||
|
timezone = None
|
||||||
|
time_format = "%Y-%m-%d %H:%M:%S"
|
||||||
|
|
||||||
|
locatime = self.timestamp_to_localtime(timestamp, timezone)
|
||||||
|
if not locatime:
|
||||||
|
return self.create_text_message(f"Invalid timestamp: {timestamp}")
|
||||||
|
|
||||||
|
localtime_format = locatime.strftime(time_format)
|
||||||
|
|
||||||
|
return self.create_text_message(f"{localtime_format}")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def timestamp_to_localtime(timestamp: int, local_tz=None) -> datetime | None:
|
||||||
|
try:
|
||||||
|
if local_tz is None:
|
||||||
|
local_tz = datetime.now().astimezone().tzinfo
|
||||||
|
if isinstance(local_tz, str):
|
||||||
|
local_tz = pytz.timezone(local_tz)
|
||||||
|
local_time = datetime.fromtimestamp(timestamp, local_tz)
|
||||||
|
return local_time
|
||||||
|
except Exception as e:
|
||||||
|
raise ToolInvokeError(str(e))
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
identity:
|
||||||
|
name: timestamp_to_localtime
|
||||||
|
author: zhuhao
|
||||||
|
label:
|
||||||
|
en_US: Timestamp to localtime
|
||||||
|
zh_Hans: 时间戳转换
|
||||||
|
description:
|
||||||
|
human:
|
||||||
|
en_US: A tool for timestamp convert to localtime
|
||||||
|
zh_Hans: 时间戳转换
|
||||||
|
llm: A tool for timestamp convert to localtime
|
||||||
|
parameters:
|
||||||
|
- name: timestamp
|
||||||
|
type: number
|
||||||
|
required: true
|
||||||
|
form: llm
|
||||||
|
label:
|
||||||
|
en_US: Timestamp
|
||||||
|
zh_Hans: 时间戳
|
||||||
|
human_description:
|
||||||
|
en_US: Timestamp
|
||||||
|
zh_Hans: 时间戳
|
||||||
|
- name: timezone
|
||||||
|
type: string
|
||||||
|
required: false
|
||||||
|
form: llm
|
||||||
|
label:
|
||||||
|
en_US: Timezone
|
||||||
|
zh_Hans: 时区
|
||||||
|
human_description:
|
||||||
|
en_US: Timezone, such as Asia/Shanghai
|
||||||
|
zh_Hans: 时区, 比如Asia/Shanghai
|
||||||
|
default: Asia/Shanghai
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue