-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from alipay/dev
feat: Release version 0.0.9
- Loading branch information
Showing
137 changed files
with
3,169 additions
and
60 deletions.
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
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
156 changes: 156 additions & 0 deletions
156
agentuniverse/agent/action/knowledge/embedding/dashscope_embedding.py
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,156 @@ | ||
# !/usr/bin/env python3 | ||
# -*- coding:utf-8 -*- | ||
|
||
# @Time : 2024/6/12 11:43 | ||
# @Author : wangchongshi | ||
# @Email : wangchongshi.wcs@antgroup.com | ||
# @FileName: dashscope_embedding.py | ||
import aiohttp | ||
import requests | ||
from typing import List, Generator, Optional | ||
import json | ||
|
||
from agentuniverse.base.util.env_util import get_from_env | ||
from agentuniverse.agent.action.knowledge.embedding.embedding import Embedding | ||
|
||
# Dashscope support max 25 string in one batch, each string max tokens is 2048. | ||
DASHSCOPE_MAX_BATCH_SIZE = 25 | ||
DASHSCOPE_EMBEDDING_URL = "https://dashscope.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding" | ||
|
||
|
||
def batched(inputs: List, | ||
batch_size: int = DASHSCOPE_MAX_BATCH_SIZE) -> Generator[List, None, None]: | ||
# Split input string list, due to dashscope support 25 strings in one call. | ||
for i in range(0, len(inputs), batch_size): | ||
yield inputs[i:i + batch_size] | ||
|
||
|
||
class DashscopeEmbedding(Embedding): | ||
"""The Dashscope embedding class.""" | ||
dashscope_api_key: Optional[str] = None | ||
|
||
def __init__(self, **kwargs): | ||
"""Initialize the dashscope embedding class, need dashscope api key.""" | ||
super().__init__(**kwargs) | ||
self.dashscope_api_key = get_from_env("DASHSCOPE_API_KEY") | ||
if not self.dashscope_api_key: | ||
raise Exception("No DASHSCOPE_API_KEY in your environment.") | ||
|
||
|
||
def get_embeddings(self, texts: List[str]) -> List[List[float]]: | ||
""" | ||
Retrieve text embeddings for a list of input texts. | ||
This function interfaces with the DashScope embedding API to obtain | ||
embeddings for a batch of input texts. It handles batching of input texts | ||
to ensure efficient API calls. Each text is processed using the specified | ||
embedding model. | ||
Args: | ||
texts (List[str]): A list of input texts to be embedded. | ||
Returns: | ||
List[List[float]]: A list of embeddings corresponding to the input texts. | ||
Raises: | ||
Exception: If the API call to DashScope fails, an exception is raised with | ||
the respective error code and message. | ||
""" | ||
def post(post_params): | ||
response = requests.post( | ||
url=DASHSCOPE_EMBEDDING_URL, | ||
headers={ | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {self.dashscope_api_key}" | ||
}, | ||
data=json.dumps(post_params, ensure_ascii=False).encode( | ||
"utf-8"), | ||
timeout=120 | ||
) | ||
resp_json = response.json() | ||
return resp_json | ||
|
||
result = [] | ||
post_params = { | ||
"model": self.embedding_model_name, | ||
"input": {}, | ||
"parameters": { | ||
"text_type": "query" | ||
} | ||
} | ||
|
||
for batch in batched(texts): | ||
post_params["input"]["texts"] = batch | ||
resp_json: dict = post(post_params) | ||
data = resp_json.get("output") | ||
if data: | ||
data = data["embeddings"] | ||
batch_result = [d['embedding'] for d in data if 'embedding' in d] | ||
result += batch_result | ||
else: | ||
error_code = resp_json.get("code", "") | ||
error_message = resp_json.get("message", "") | ||
raise Exception(f"Failed to call dashscope embedding api, " | ||
f"error code:{error_code}, " | ||
f"error message:{error_message}") | ||
return result | ||
|
||
async def async_get_embeddings(self, texts: List[str]) -> List[List[float]]: | ||
""" | ||
Async version of get_embeddings. | ||
This function interfaces with the DashScope embedding API to obtain | ||
embeddings for a batch of input texts. It handles batching of input texts | ||
to ensure efficient API calls. Each text is processed using the specified | ||
embedding model. | ||
Args: | ||
texts (List[str]): A list of input texts to be embedded. | ||
Returns: | ||
List[List[float]]: A list of embeddings corresponding to the input texts. | ||
Raises: | ||
Exception: If the API call to DashScope fails, an exception is raised with | ||
the respective error code and message. | ||
""" | ||
async def async_post(post_params): | ||
async with aiohttp.ClientSession() as session: | ||
async with await session.post( | ||
url=DASHSCOPE_EMBEDDING_URL, | ||
headers={ | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {self.dashscope_api_key}" | ||
}, | ||
data=json.dumps(post_params, ensure_ascii=False).encode( | ||
"utf-8"), | ||
timeout=120, | ||
) as resp: | ||
resp_json = await resp.json() | ||
return resp_json | ||
|
||
result = [] | ||
post_params = { | ||
"model": self.embedding_model_name, | ||
"input": {}, | ||
"parameters": { | ||
"text_type": "query" | ||
} | ||
} | ||
|
||
for batch in batched(texts): | ||
post_params["input"]["texts"] = batch | ||
resp_json: dict = await async_post(post_params) | ||
data = resp_json.get("output") | ||
if data: | ||
data = data["embeddings"] | ||
batch_result = [d['embedding'] for d in data if | ||
'embedding' in d] | ||
result += batch_result | ||
else: | ||
error_code = resp_json.get("code", "") | ||
error_message = resp_json.get("message", "") | ||
raise Exception(f"Failed to call dashscope embedding api, " | ||
f"error code:{error_code}, " | ||
f"error message:{error_message}") | ||
return result |
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
Empty file.
27 changes: 27 additions & 0 deletions
27
agentuniverse/agent/default/nl2api_agent/default_cn_prompt.yaml
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,27 @@ | ||
introduction: 你是一位精通工具选择ai助手。 | ||
target: 你的目标是根据用户的问题选择出合适的工具。 | ||
instruction: | | ||
你需要根据问题和用户提供的工具,选择其中的一个或几个工具用来回答用户提出的问题。 | ||
你必须从多个角度、维度分析用户的问题,需要根据背景和问题,决定使用哪些工具可以回答用户问题。 | ||
您可以使用以下工具: | ||
{tools} | ||
之前的对话: | ||
{chat_history} | ||
背景信息是: | ||
{background} | ||
回答必须是按照以下格式化的Json代码片段。 | ||
1. tools字段代表选择的几个工具的完整名称,列表格式。例如:[add, sub, mul, div] | ||
2. thought字段代表选择工具的思考过程和原因。 | ||
```{{ | ||
"tools": list, | ||
"thought": string | ||
}}``` | ||
当前的问题:{input} | ||
metadata: | ||
type: 'PROMPT' | ||
version: 'default_nl2api_agent.cn' |
26 changes: 26 additions & 0 deletions
26
agentuniverse/agent/default/nl2api_agent/default_en_prompt.yaml
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,26 @@ | ||
introduction: You are an AI assistant proficient in tool selection. | ||
target: Your goal is to select the appropriate tools based on the user's questions. | ||
instruction: | | ||
Your task is to select one or several tools from those provided by the user, based on their question and the context, in order to answer the user's query. | ||
You must analyze the user's problem from multiple angles and dimensions, taking into account the background and context of the question, and decide which tools can be used to answer the user's question. | ||
You may use the following tools: | ||
{tools} | ||
Previous conversation: | ||
{chat_history} | ||
The background information is: | ||
{background} | ||
The response must follow the format below as a formatted JSON code snippet. | ||
1. The tools field represents the full names of the selected tools in a list format, such as:[add, sub, mul, div] | ||
2. The thought field represents the thinking process and reasons behind the selection of tools. | ||
```{{ | ||
"tools": list, | ||
"thought": string | ||
}}``` | ||
Question: {input} | ||
metadata: | ||
type: 'PROMPT' | ||
version: 'default_nl2api_agent.en' |
Oops, something went wrong.