Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
# Conflicts:
#	web/src/pages/flow/canvas/node/retrieval-node.tsx
  • Loading branch information
isthaison committed Dec 4, 2024
2 parents efb4481 + 84afb42 commit d6e9f70
Show file tree
Hide file tree
Showing 24 changed files with 1,603 additions and 130 deletions.
10 changes: 9 additions & 1 deletion conf/mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,21 @@
}
},
{
"string": {
"rank_feature": {
"match": "*_fea",
"mapping": {
"type": "rank_feature"
}
}
},
{
"rank_features": {
"match": "*_feas",
"mapping": {
"type": "rank_features"
}
}
},
{
"dense_vector": {
"match": "*_512_vec",
Expand Down
1,160 changes: 1,159 additions & 1 deletion docs/references/http_api_reference.md

Large diffs are not rendered by default.

121 changes: 120 additions & 1 deletion docs/references/python_api_reference.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
from sdk.python.ragflow_sdk import Agentfrom docutils.nodes import titlefrom sdk.python.ragflow_sdk import Agent---
sidebar_position: 2
slug: /python_api_reference
---
Expand Down Expand Up @@ -1513,4 +1513,123 @@ while True:
for ans in session.ask(question, stream=True):
print(ans.content[len(cont):], end='', flush=True)
cont = ans.content
```
---

## List agent sessions

```python
Agent.list_sessions(
agent_id,
rag
page: int = 1,
page_size: int = 30,
orderby: str = "update_time",
desc: bool = True,
id: str = None
) -> List[Session]
```

Lists sessions associated with the current agent.

### Parameters

#### page: `int`

Specifies the page on which the sessions will be displayed. Defaults to `1`.

#### page_size: `int`

The number of sessions on each page. Defaults to `30`.

#### orderby: `str`

The field by which sessions should be sorted. Available options:

- `"create_time"`
- `"update_time"`(default)

#### desc: `bool`

Indicates whether the retrieved sessions should be sorted in descending order. Defaults to `True`.

#### id: `str`

The ID of the agent session to retrieve. Defaults to `None`.


### Returns

- Success: A list of `Session` objects associated with the current agent.
- Failure: `Exception`.

### Examples

```python
from ragflow_sdk import RAGFlow

rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
agent_id = "2710f2269b4611ef8fdf0242ac120006"
sessions=Agent.list_sessions(agent_id,rag_object)
for session in sessions:
print(session)
```

---
## List agents

```python
RAGFlow.list_agents(
page: int = 1,
page_size: int = 30,
orderby: str = "create_time",
desc: bool = True,
id: str = None,
title: str = None
) -> List[Agent]
```

Lists agents.

### Parameters

#### page: `int`

Specifies the page on which the agents will be displayed. Defaults to `1`.

#### page_size: `int`

The number of agents on each page. Defaults to `30`.

#### orderby: `str`

The attribute by which the results are sorted. Available options:

- `"create_time"` (default)
- `"update_time"`

#### desc: `bool`

Indicates whether the retrieved agents should be sorted in descending order. Defaults to `True`.

#### id: `str`

The ID of the agent to retrieve. Defaults to `None`.

#### name: `str`

The name of the agent to retrieve. Defaults to `None`.

### Returns

- Success: A list of `Agent` objects.
- Failure: `Exception`.

### Examples

```python
from ragflow_sdk import RAGFlow
rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
for agent in rag_object.list_agents():
print(agent)
```
25 changes: 16 additions & 9 deletions rag/nlp/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def need_fine_grained_tokenize(tk):
keywords.append(tt)
twts = self.tw.weights([tt])
syns = self.syn.lookup(tt)
if syns: keywords.extend(syns)
if syns and len(keywords) < 32: keywords.extend(syns)
logging.debug(json.dumps(twts, ensure_ascii=False))
tms = []
for tk, w in sorted(twts, key=lambda x: x[1] * -1):
Expand All @@ -140,17 +140,24 @@ def need_fine_grained_tokenize(tk):
sm = [FulltextQueryer.subSpecialChar(m) for m in sm if len(m) > 1]
sm = [m for m in sm if len(m) > 1]

keywords.append(re.sub(r"[ \\\"']+", "", tk))
keywords.extend(sm)
if len(keywords) >= 12:
break
if len(keywords) < 32:
keywords.append(re.sub(r"[ \\\"']+", "", tk))
keywords.extend(sm)

tk_syns = self.syn.lookup(tk)
tk_syns = [FulltextQueryer.subSpecialChar(s) for s in tk_syns]
if len(keywords) < 32: keywords.extend([s for s in tk_syns if s])
tk_syns = [rag_tokenizer.fine_grained_tokenize(s) for s in tk_syns if s]
tk_syns = [f"\"{s}\"" if s.find(" ")>0 else s for s in tk_syns]

if len(keywords) >= 32:
break

tk = FulltextQueryer.subSpecialChar(tk)
if tk.find(" ") > 0:
tk = '"%s"' % tk
if tk_syns:
tk = f"({tk} %s)" % " ".join(tk_syns)
tk = f"({tk} OR (%s)^0.2)" % " ".join(tk_syns)
if sm:
tk = f'{tk} OR "%s" OR ("%s"~2)^0.5' % (" ".join(sm), " ".join(sm))
if tk.strip():
Expand All @@ -159,14 +166,14 @@ def need_fine_grained_tokenize(tk):
tms = " ".join([f"({t})^{w}" for t, w in tms])

if len(twts) > 1:
tms += ' ("%s"~4)^1.5' % (" ".join([t for t, _ in twts]))
tms += ' ("%s"~2)^1.5' % rag_tokenizer.tokenize(tt)
if re.match(r"[0-9a-z ]+$", tt):
tms = f'("{tt}" OR "%s")' % rag_tokenizer.tokenize(tt)

syns = " OR ".join(
[
'"%s"^0.7'
% FulltextQueryer.subSpecialChar(rag_tokenizer.tokenize(s))
'"%s"'
% rag_tokenizer.tokenize(FulltextQueryer.subSpecialChar(s))
for s in syns
]
)
Expand Down
16 changes: 16 additions & 0 deletions sdk/python/ragflow_sdk/modules/agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .base import Base
from .session import Session
import requests
from typing import List

class Agent(Base):
def __init__(self,rag,res_dict):
Expand Down Expand Up @@ -57,3 +58,18 @@ def create_session(id,rag) -> Session:
return Session(rag,res.get("data"))
raise Exception(res.get("message"))

@staticmethod
def list_sessions(agent_id,rag,page: int = 1, page_size: int = 30, orderby: str = "create_time", desc: bool = True,
id: str = None) -> List[Session]:
url = f"{rag.api_url}/agents/{agent_id}/sessions"
headers = {"Authorization": f"Bearer {rag.user_key}"}
params = {"page": page, "page_size": page_size, "orderby": orderby, "desc": desc, "id": id}
res = requests.get(url=url,headers=headers,params=params)
res = res.json()
if res.get("code") == 0:
result_list = []
for data in res.get("data"):
temp_agent = Session(rag,data)
result_list.append(temp_agent)
return result_list
raise Exception(res.get("message"))
4 changes: 2 additions & 2 deletions sdk/python/ragflow_sdk/modules/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ def __init__(self, rag, res_dict):
self.id = None
self.name = "New session"
self.messages = [{"role": "assistant", "content": "Hi! I am your assistant,can I help you?"}]
self.chat_id = None
self.agent_id = None
for key,value in res_dict.items():
if key =="chat_id" and value is not None:
self.chat_id = None
self.__session_type = "chat"
if key == "agent_id" and value is not None:
self.agent_id = None
self.__session_type = "agent"
super().__init__(rag, res_dict)

Expand Down
19 changes: 17 additions & 2 deletions sdk/python/ragflow_sdk/ragflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .modules.chat import Chat
from .modules.chunk import Chunk
from .modules.dataset import DataSet
from .modules.agent import Agent


class RAGFlow:
Expand Down Expand Up @@ -86,8 +87,10 @@ def list_datasets(self, page: int = 1, page_size: int = 30, orderby: str = "crea
return result_list
raise Exception(res["message"])

def create_chat(self, name: str, avatar: str = "", dataset_ids: list[str] = [],
llm: Chat.LLM | None = None, prompt: Chat.Prompt | None = None) -> Chat:
def create_chat(self, name: str, avatar: str = "", dataset_ids=None,
llm: Chat.LLM | None = None, prompt: Chat.Prompt | None = None) -> Chat:
if dataset_ids is None:
dataset_ids = []
dataset_list = []
for id in dataset_ids:
dataset_list.append(id)
Expand Down Expand Up @@ -176,3 +179,15 @@ def retrieve(self, dataset_ids, document_ids=None, question="", page=1, page_siz
chunks.append(chunk)
return chunks
raise Exception(res.get("message"))


def list_agents(self, page: int = 1, page_size: int = 30, orderby: str = "update_time", desc: bool = True,
id: str | None = None, title: str | None = None) -> list[Agent]:
res = self.get("/agents",{"page": page, "page_size": page_size, "orderby": orderby, "desc": desc, "id": id, "title": title})
res = res.json()
result_list = []
if res.get("code") == 0:
for data in res['data']:
result_list.append(Agent(self, data))
return result_list
raise Exception(res["message"])
9 changes: 9 additions & 0 deletions sdk/python/test/test_sdk_api/t_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from ragflow_sdk import RAGFlow,Agent
from common import HOST_ADDRESS
import pytest

@pytest.mark.skip(reason="")
def test_list_agents_with_success(get_api_key_fixture):
API_KEY=get_api_key_fixture
rag = RAGFlow(API_KEY,HOST_ADDRESS)
rag.list_agents()
1 change: 1 addition & 0 deletions sdk/python/test/test_sdk_api/t_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ def test_delete_datasets_with_success(get_api_key_fixture):
def test_list_datasets_with_success(get_api_key_fixture):
API_KEY = get_api_key_fixture
rag = RAGFlow(API_KEY, HOST_ADDRESS)
rag.create_dataset("test_list_datasets")
rag.list_datasets()
9 changes: 8 additions & 1 deletion sdk/python/test/test_sdk_api/t_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,11 @@ def test_create_agent_conversation_with_success(get_api_key_fixture):
API_KEY = "ragflow-BkOGNhYjIyN2JiODExZWY5MzVhMDI0Mm"
rag = RAGFlow(API_KEY,HOST_ADDRESS)
session = Agent.create_session("2e45b5209c1011efa3e90242ac120006", rag)
session.ask("What is this job")
session.ask("What is this job")

@pytest.mark.skip(reason="")
def test_list_agent_sessions_with_success(get_api_key_fixture):
API_KEY = "ragflow-BkOGNhYjIyN2JiODExZWY5MzVhMDI0Mm"
agent_id = "2710f2269b4611ef8fdf0242ac120006"
rag = RAGFlow(API_KEY,HOST_ADDRESS)
Agent.list_sessions(agent_id,rag)
4 changes: 2 additions & 2 deletions web/src/components/knowledge-base-item.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useTranslate } from '@/hooks/common-hooks';
import { useNextFetchKnowledgeList } from '@/hooks/knowledge-hooks';
import { useFetchKnowledgeList } from '@/hooks/knowledge-hooks';
import { UserOutlined } from '@ant-design/icons';
import { Avatar, Form, Select, Space } from 'antd';

const KnowledgeBaseItem = () => {
const { t } = useTranslate('chat');

const { list: knowledgeList } = useNextFetchKnowledgeList(true);
const { list: knowledgeList } = useFetchKnowledgeList(true);

const knowledgeOptions = knowledgeList.map((x) => ({
label: (
Expand Down
39 changes: 39 additions & 0 deletions web/src/components/ui/skeleton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { cn } from '@/lib/utils';

function Skeleton({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) {
return (
<div
className={cn('animate-pulse rounded-md bg-muted', className)}
{...props}
/>
);
}

function ParagraphSkeleton() {
return (
<div className="flex items-center space-x-4">
<Skeleton className="h-12 w-12 rounded-full" />
<div className="space-y-2">
<Skeleton className="h-4 w-[250px]" />
<Skeleton className="h-4 w-[200px]" />
</div>
</div>
);
}

function CardSkeleton() {
return (
<div className="flex flex-col space-y-3">
<Skeleton className="h-[125px] w-[250px] rounded-xl" />
<div className="space-y-2">
<Skeleton className="h-4 w-[250px]" />
<Skeleton className="h-4 w-[200px]" />
</div>
</div>
);
}

export { CardSkeleton, ParagraphSkeleton, Skeleton };
2 changes: 1 addition & 1 deletion web/src/hooks/knowledge-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const useFetchKnowledgeBaseConfiguration = () => {
return { data, loading };
};

export const useNextFetchKnowledgeList = (
export const useFetchKnowledgeList = (
shouldFilterListWithoutDocument: boolean = false,
): {
list: IKnowledge[];
Expand Down
Loading

0 comments on commit d6e9f70

Please sign in to comment.