Skip to content
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

API: retrieval api #1763

Merged
merged 3 commits into from
Aug 1, 2024
Merged

API: retrieval api #1763

merged 3 commits into from
Aug 1, 2024

Conversation

Valdanitooooo
Copy link
Contributor

@Valdanitooooo Valdanitooooo commented Aug 1, 2024

What problem does this PR solve?

Add retrieval api on a specific knowledge base

ragflow

#1102

Type of change

  • New Feature (non-breaking change which adds functionality)

@KevinHuSh KevinHuSh merged commit b9a50ef into infiniflow:main Aug 1, 2024
1 check passed
@Valdanitooooo
Copy link
Contributor Author

Valdanitooooo commented Aug 1, 2024

Now, you can use RagFlow's retrieval API as a tool in your LLM Agent. 😉

import json
import os

import requests
from dotenv import load_dotenv

load_dotenv()

RAGFLOW_API_KEY = os.environ.get("RAGFLOW_API_KEY", "")
RAGFLOW_BASE_URL = os.environ.get("RAGFLOW_BASE_URL", "")

headers = {
    'Authorization': f'Bearer {RAGFLOW_API_KEY}',
    'Content-Type': 'application/json',
}

def ragflow_api_retrieval(kb_id, doc_count, prompt):
    payload = {
        "similarity_threshold": 0.2,
        "vector_similarity_weight": 0.3,
        "question": prompt,
        "doc_ids": [],
        "kb_id": kb_id,
        "page": 1,
        "size": doc_count
    }
    response = requests.post(
        RAGFLOW_BASE_URL + "/api/retrieval",
        headers={**headers},
        data=json.dumps(payload)
    )
    if response.status_code == 200:
        try:
            data = response.json()
            # print(data)
            chunks = data['data']['chunks']
            if len(chunks) > 0:
                docs_str = ""
                for chunk in chunks:
                    docs_str += chunk["content_ltks"].replace("\r", "\n") + "\n"
            return docs_str
        except Exception as e:
            print(str(e))
    else:
        print(f"Request failed with status code {response.status_code}")
    return "No data found"

Halfknow pushed a commit to Halfknow/ragflow that referenced this pull request Nov 11, 2024
### What problem does this PR solve?

Add retrieval api on a specific knowledge base


![ragflow](https://github.com/user-attachments/assets/dc30a4c3-03c5-4d34-bb7c-60b8830f1225)

infiniflow#1102

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants