myst | ||||||||
---|---|---|---|---|---|---|---|---|
|
As mentioned in the tutorial, you can run api server as follows:
from autorag.deploy import ApiRunner
import nest_asyncio
nest_asyncio.apply()
runner = ApiRunner.from_yaml('your/path/to/pipeline.yaml', project_dir='your/project/directory')
runner.run_api_server()
or
from autorag.deploy import ApiRunner
import nest_asyncio
nest_asyncio.apply()
runner = ApiRunner.from_trial_folder('/your/path/to/trial_dir')
runner.run_api_server()
autorag run_api --trial_dir /trial/dir/0 --host 0.0.0.0 --port 8000
For accessing the API server from the public, you can use the NGrok tunnel service. It automatically creates ngrok tunnel to your local server.
You can see the logs of the public URL like below:
INFO [api.py:199] >> Public API URL: api.py:199
https://8a31-14-52-132-205.ngrok-free.app
This is the URL to your local server, so use it as the host at request.
- Summary: Run a query and get generated text with retrieved passages.
- Request Body:
- Content Type:
application/json
- Schema:
- Properties:
query
(string, required): The query string.result_column
(string, optional): The result column name. Default isgenerated_texts
.
- Properties:
- Content Type:
- Responses:
- 200 OK:
- Content Type:
application/json
- Schema:
- Properties:
result
(string or array of strings): The result text or list of texts.retrieved_passage
(array of objects): List of retrieved passages.- Properties:
content
(string): The content of the passage.doc_id
(string): Document ID.filepath
(string, nullable): File path.file_page
(integer, nullable): File page number.start_idx
(integer, nullable): Start index.end_idx
(integer, nullable): End index.
- Properties:
- Properties:
- Content Type:
- 200 OK:
- Summary: Stream generated text with retrieved passages.
- Description: This endpoint streams the generated text line by line. The
retrieved_passage
is sent first, followed by theresult
streamed incrementally. - Request Body:
- Content Type:
application/json
- Schema:
- Properties:
query
(string, required): The query string.result_column
(string, optional): The result column name. Default isgenerated_texts
.
- Properties:
- Content Type:
- Responses:
- 200 OK:
- Content Type:
text/event-stream
- Schema:
- Properties:
type
(generated_text or retrieved_passage): If it is generated_text, you can see only the generated text. If it is retrieved_passage, you can see the retrieved passage and passage_index.generated_text
(string): The generated text from the generator (LLM). The result of the RAG system.retrieved_passage
(object): Retrieved passage.- Properties:
content
(string): The content of the passage.doc_id
(string): Document ID.filepath
(string, nullable): File path.file_page
(integer, nullable): File page number.start_idx
(integer, nullable): Start index.end_idx
(integer, nullable): End index.
- Properties:
passage_index
(integer): Index of the retrieved passage.
- Properties:
- Content Type:
- 200 OK:
- Summary: Get the API version.
- Description: Returns the current version of the API as a string.
- Responses:
- 200 OK:
- Content Type:
application/json
- Schema:
- Properties:
version
(string): The version of the API.
- Properties:
- Content Type:
- 200 OK:
Certainly! Below, I'll provide both Python sample code using the requests
library and a curl
command for each of the API endpoints described in the OpenAPI specification.
First, ensure you have the requests
library installed. You can install it using pip if you haven't already:
pip install requests
Here's the Python client code for each endpoint:
import requests
from autorag.utils.util import decode_multiple_json_from_bytes
# Base URL of the API
BASE_URL = "http://example.com:8000" # Replace with the actual base URL of the API
def run_query(query, result_column="generated_texts"):
url = f"{BASE_URL}/v1/run"
payload = {
"query": query,
"result_column": result_column
}
response = requests.post(url, json=payload)
if response.status_code == 200:
return response.json()
else:
response.raise_for_status()
def stream_query(query, result_column="generated_texts"):
url = f"{BASE_URL}/v1/stream"
payload = {
"query": query,
"result_column": result_column
}
with requests.Session() as session:
response = session.post(url, json=payload, stream=True)
retrieved_passages = [] # This will store retrieved passages
# Check if the request was successful
if response.status_code == 200:
# Process the streaming response
for i, chunk in enumerate(response.iter_content(chunk_size=None)):
if chunk:
data_list = decode_multiple_json_from_bytes(chunk)
for data in data_list:
if data["type"] == "retrieved_passage":
retrieved_passages.append(data["retrieved_passage"])
else:
print(data["generated_text"], end="") # Stream the generated texts
else:
print(f"Request failed with status code: {response.status_code}")
print(f"Response content: {response.text}")
def get_version():
url = f"{BASE_URL}/version"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
response.raise_for_status()
# Example usage
if __name__ == "__main__":
# Run a query
result = run_query("example query")
print("Run Query Result:", result)
# Stream a query
print("Stream Query Result:")
stream_query("example query")
# Get API version
version = get_version()
print("API Version:", version)
Here are the equivalent curl
commands for each endpoint:
curl -X POST "http://example.com/v1/run" \
-H "Content-Type: application/json" \
-d '{"query": "example query", "result_column": "generated_texts"}'
curl -X POST "http://example.com/v1/stream" \
-H "Content-Type: application/json" \
-d '{"query": "example query", "result_column": "generated_texts"}' \
--no-buffer
curl -X GET "http://example.com/version"