-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding nilai_chat_completion function and readme clean up
Co-authored-by: Dimitris Mouris <[email protected]>
- Loading branch information
Showing
9 changed files
with
612 additions
and
389 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,49 @@ | ||
import requests | ||
""" | ||
Example of querying nilDB with NilAI using nilRAG. | ||
""" | ||
|
||
import os | ||
import sys | ||
import json | ||
from nilrag.nildb_requests import NilDB, Node | ||
|
||
|
||
JSON_FILE = "examples/query_nildb_config.json" | ||
|
||
# Load NilDB from JSON file if it exists | ||
if os.path.exists(JSON_FILE): | ||
print("Loading NilDB configuration from file...") | ||
with open(JSON_FILE, "r", encoding="utf-8") as f: | ||
data = json.load(f) | ||
nodes = [] | ||
for node_data in data["nodes"]: | ||
nodes.append( | ||
Node( | ||
url=node_data["url"], | ||
node_id=None, | ||
org=None, | ||
bearer_token=node_data.get("bearer_token"), | ||
schema_id=node_data.get("schema_id"), | ||
diff_query_id=node_data.get("diff_query_id"), | ||
) | ||
) | ||
nilDB = NilDB(nodes) | ||
else: | ||
print("Error: NilDB configuration file not found.") | ||
sys.exit(1) | ||
|
||
def query_rag_system(query_text: str, base_url: str = "http://localhost:8000") -> dict: | ||
""" | ||
Send a query to the RAG system's FastAPI endpoint. | ||
Args: | ||
query_text (str): The query text to send | ||
base_url (str): Base URL of the FastAPI server | ||
Returns: | ||
dict: The response from the server | ||
""" | ||
# Construct the endpoint URL | ||
endpoint = f"{base_url}/process-client-query" | ||
|
||
# Prepare the request payload | ||
payload = { | ||
"query": query_text | ||
} | ||
|
||
# Set headers for JSON content | ||
headers = { | ||
"accept": "application/json", | ||
"Content-Type": "application/json" | ||
} | ||
|
||
try: | ||
# Send POST request | ||
response = requests.post(endpoint, json=payload, headers=headers) | ||
|
||
# Raise an exception for bad status codes | ||
response.raise_for_status() | ||
|
||
# Return the JSON response | ||
return response.json() | ||
|
||
except requests.exceptions.RequestException as e: | ||
print(f"Error making request: {e}") | ||
return None | ||
print("NilDB instance:", nilDB) | ||
print() | ||
|
||
if __name__ == "__main__": | ||
# Example query | ||
query = "Tell me about places in Asia." | ||
|
||
# Make the request | ||
result = query_rag_system(query) | ||
|
||
# Print the result | ||
if result: | ||
print("Response from server:") | ||
print(json.dumps(result, indent=2)) | ||
print('Query nilAI with nilRAG...') | ||
response = nilDB.nilai_chat_completion( | ||
nilai_url="http://127.0.0.1:8080/", | ||
token="1770c101-dd83-4fbc-b996-ef8121889172", | ||
messages=[ | ||
{"role": "user", "content": "Tell me about Asia."} | ||
], | ||
temperature=0.2, | ||
max_tokens=2048, | ||
stream=False, | ||
) | ||
print(response) |
Oops, something went wrong.