打造智能语料库:通过Coco AI Server 实现 Notion 笔记

忘机山人

发布于294天前
博客图片修整中,看不了可以先搜索公众号“忘机山人”看。
## CoCo Server 部署 RAG,使用 Notion 作为语料库(上)


https://appstore.lazycat.cloud/#/shop/detail/xu.deploy.coco-ai


1. 启动 Easysearch,这里把 Easysearch 作为语料库,把 notion 的素材存在 Easysearch

2. 启动 ollama,使用 LLM 进行推理

3. 启动 Coco Server,端口在 9000
   
   ![image-20250217142649790](https://raw.githubusercontent.com/Xu-Hardy/picgo-imh/master/image-20250217142649790.png)

Coco App 连接 Sever,输入输入

![image-20250217142816787](https://raw.githubusercontent.com/Xu-Hardy/picgo-imh/master/image-20250217142816787.png)

登录自己 server,依旧使用 Github 登录

![image-20250217142837884](https://raw.githubusercontent.com/Xu-Hardy/picgo-imh/master/image-20250217142837884.png)

Github 登录之后的重定向,我们目前需要抓取最后的, 后面用这个 token 换取访问 Coco Server AI 的 key:

```
coco://oauth_callback?code=cupibub55o1cfqbveps0q804ai6aj14in3u91xjhvuk8s7ixirjsq2j9mmyyeut91nmgjwz0b494ngpk&request_id=eb94762b-f054-4710-9c6cf20889d3&provider=coco-cloud

```

![image-20250217142925920](https://raw.githubusercontent.com/Xu-Hardy/picgo-imh/master/image-20250217142925920.png)

认证步骤如下:

第一步:
http://localhost:9000/sso/login/github?provider=coco-cloud&product=coco&request_id=dd9825e1-ebd3-4c84-9e3f-7ccb0421c508

会返回一个 Token,记录下来,这个是只是临时的, 如 XXABC

第二步:
curl -H'X-API-TOKEN: XXABC' "http://localhost:9000/auth/request_access_token?request_id=dd9825e1-ebd3-4c84-9e3f-7ccb0421c508"

返回的才是你要的 Token

在 postman 中换 token,得到 access_token 和过期时间:

![image-20250217143134946](https://raw.githubusercontent.com/Xu-Hardy/picgo-imh/master/image-20250217143134946.png)

```python
import requests

def parse_oauth_callback(url):
    query_params = {param.split('=')[0]: param.split('=')[1] for param in url.split('?')[1].split('&')}
    code = query_params.get("code")
    request_id = query_params.get("request_id")
    return code, request_id

def request_access_token(code, base_url, request_id):
    url = f"{base_url}/auth/request_access_token?request_id={request_id}"
    headers = {"X-API-TOKEN": code}
    response = requests.get(url, headers=headers)
    return response.json()

# 示例输入
oauth_callback_url = """
coco://oauth_callback?code=cupibub55o1cfqbveps0q804ai6aj151wu4in3u91xjhvuk8s7ixirjsq2j9mmyyeut91nmgjwz0b494ngpk&request_id=eb94762b-f054-4710-9c6a-0cf2088729d3&provider=coco-cloud
"""
base_url = "http://localhost:9000"

# 解析 code 和 request_id
code, request_id = parse_oauth_callback(oauth_callback_url)

# 发送请求
token_response = request_access_token(code, base_url, request_id)
print(token_response)

```

可以用 access_key 查看用户信息:

![image-20250217143244767](https://raw.githubusercontent.com/Xu-Hardy/picgo-imh/master/image-20250217143244767.png)

```python
import requests

url = "http://localhost:9000/account/profile"

payload = {}
headers = {
  'X-API-TOKEN': 'cupichb55o1cfqbveq90zwomyxs791ul3esbxxt480c8dzgvdtjtvmcnsld4a5v0wvx9l9ofcf1'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

```

注册 Notion connector:

```python
import requests
import json

def update_connector(base_url, api_token, connector_name, data):
    url = f"{base_url}/connector/{connector_name}?replace=true"
    headers = {
        "X-API-TOKEN": api_token,
        "Content-Type": "application/json"
    }
    response = requests.put(url, headers=headers, data=json.dumps(data))
    return response.json()


base_url = "http://localhost:9000"
api_token = ""


notion_data = {
    "name": "Notion Docs Connector",
    "description": "Fetch the docs metadata for notion.",
    "icon": "/assets/connector/notion/icon.png",
    "category": "website",
    "tags": ["docs", "notion", "web"],
    "url": "http://coco.rs/connectors/notion",
    "assets": {
        "icons": {
            "default": "/assets/connector/notion/icon.png",
            "web_page": "/assets/connector/notion/icon.png",
            "database": "/assets/connector/notion/database.png",
            "page": "/assets/connector/notion/page.png"
        }
    }
}


response_notion = update_connector(base_url, api_token, "notion", notion_data)
print(response_notion)
```

修改 Notion 配置文件,激活检索 Notion:

![image-20250217210840104](https://raw.githubusercontent.com/Xu-Hardy/picgo-imh/master/image-20250217210840104.png)

在 Notion 这个网站申请 API key,https://www.notion.so/profile/integrations

![image-20250217212252851](https://raw.githubusercontent.com/Xu-Hardy/picgo-imh/master/image-20250217212252851.png)

配置完成之后,设置权限和展示 apikey

![image-20250217212349650](https://raw.githubusercontent.com/Xu-Hardy/picgo-imh/master/image-20250217212349650.png)

配置 Notion Connector,这里需要用到 Notion 的 API Key:

```python
import requests
import json

def create_datasource(base_url, api_token, data):
    url = f"{base_url}/datasource/"
    headers = {
        "X-API-TOKEN": api_token,
        "Content-Type": "application/json"
    }
    response = requests.post(url, headers=headers, data=json.dumps(data))
    return response.json()

# 示例输入
base_url = "http://localhost:9000"
api_token = ""

datasource_data = {
    "name": "My Notion",
    "type": "connector",
    "connector": {
        "id": "notion",
        "config": {
            "token": ""
        }
    }
}

# 发送 POST 请求
response = create_datasource(base_url, api_token, datasource_data)
print(response)

```

需要在 Notion 中设置集成,这样 Coco Server 才会搜索到:

![image-20250217212508943](https://raw.githubusercontent.com/Xu-Hardy/picgo-imh/master/image-20250217212508943.png)

Coco server 日志中可以检索到 notion 了:

![image-20250217210810825](https://raw.githubusercontent.com/Xu-Hardy/picgo-imh/master/image-20250217210810825.png)

终于可以在搜索栏检索到了。

![image-20250217211620791](https://raw.githubusercontent.com/Xu-Hardy/picgo-imh/master/image-20250217211620791.png)

评论

0

暂无评论

说点什么呢~
收藏
0
0
0