Skip to content

Commit

Permalink
cleanup docs / api (#34)
Browse files Browse the repository at this point in the history
* cleanup docs / api

* polish

* tweaks
  • Loading branch information
emrgnt-cmplxty authored Jan 14, 2024
1 parent 51f473e commit 01848d4
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 145 deletions.
85 changes: 43 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export SCIPHI_API_KEY=$MY_SCIPHI_API_KEY

### Usage

Code your own search agent workflow:

Call a pre-configured search agent endpoint:

```python
# Requires SCIPHI_API_KEY in the environment
from agent_search import SciPhi

client = SciPhi()
Expand All @@ -42,61 +42,62 @@ print(agent_summary)
# { 'response': '...', 'other_queries': '...', 'search_results': '...' }
```

Code your own search agent workflow:
Standalone searches and from the AgentSearch search engine are supported:

```python
# Requires SCIPHI_API_KEY in the environment
from agent_search import SciPhi

...
client = SciPhi()

# Perform a search
search_response = client.search(query='Quantum Field Theory', search_provider='agent-search')

print(search_response)
# [{ 'score': '.89', 'url': 'https://...', 'metadata': {...} }]
```

Code your own custom search agent workflow:

```python
# Requires SCIPHI_API_KEY in the environment
from agent_search import SciPhi

client = SciPhi()

# Specify instructions for the task
instruction = "Your task is to perform retrieval augmented generation (RAG) over the given query and search results. Return your answer in a json format that includes a summary of the search results and a list of related queries."
query = "What is Fermat's Last Theorem?"

# construct search context
search_response = client.search(query=query, search_provider='agent-search')
search_context = "\n\n".join(
f"{idx + 1}. Title: {item['title']}\nURL: {item['url']}\nText: {item['text']}"
for idx, item in enumerate(search_response)
).encode('utf-8')

# Prefix to enforce a JSON response
json_response_prefix = '{"summary":'

# Prepare a prompt
formatted_prompt = f"### Instruction:{instruction}\n\nQuery:\n{query}\n\nSearch Results:\n${search_context}\n\nQuery:\n{query}\n### Response:\n{json_response_prefix}",

# Generate a completion with Sensei-7B-V1
completion = json_response_prefix + client.completion(formatted_prompt, llm_model_name="SciPhi/Sensei-7B-V1")

print(completion)
# {
# "summary": "\nFermat's Last Theorem is a mathematical proposition first prop ... ",
# "other_queries": ["The role of elliptic curves in the proof of Fermat's Last Theorem", ...]
# }
```

## Community & Support

- **Engage with Us:** Join our [Discord community](#) for discussions and updates.
- **Engage with Us:** Join our [Discord community](https://discord.gg/mN4kWbsgRu) for discussions and updates.
- **Feedback & Inquiries:** Contact us via email for personalized support.

## Self-Hosting Guide

AgentSearch is a multi-TB dataset curated by SciPhi and accessible [on HuggingFace](https://huggingface.co/datasets/SciPhi/AgentSearch-V1). This repository has the necessary code for individuals to download and host their own search engine with this dataset.

> **Warning:** This setup documentation is preliminary and not yet finalized. Please note that the setup process may change in the future.
### Prerequisites

- Docker: [Download here](#)
- Postgres: [Download here](#)

### Setup Steps

1. **Start Postgres Database**
```bash
sudo service postgresql start
```
2. **Populate Database**
```bash
# Needs revision.
# python -m agent_search.scripts.populate_postgres_from_hf run
```
3. **Qdrant Service with Docker**
```bash
# Needs revision.
# docker run -p 6333:6333 -p 6334:6334 -v $(pwd)/qdrant_storage:/qdrant/storage:z qdrant/qdrant
```
4. **Populate Vector Database**
```bash
python -m agent_search.scripts.populate_qdrant_from_postgres run --delete_existing=True
```
5. **Launch Server**
```bash
python -m agent_search.app.server
```

### Additional Notes

- Execute commands from the root directory of the AgentSearch project.
- Replace `query` in the run command with your specific search query.
- User Guide coming soon!
30 changes: 13 additions & 17 deletions agent_search/providers/sciphi.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,25 +281,21 @@ def completion(
from synthesizer.interface import LLMInterfaceManager
from synthesizer.llm import GenerationConfig

llm_interface = LLMInterfaceManager.get_interface_from_args(
LLMProviderName("sciphi"),
)

generation_config = GenerationConfig(
model_name=llm_model_name,
max_tokens_to_sample=llm_max_tokens_to_sample,
temperature=llm_temperature,
top_p=llm_top_p,
)

completion = '{"response":' + llm_interface.get_completion(
prompt, generation_config
).replace("</s>", "")
try:
completion = json.loads(completion)
llm_interface = LLMInterfaceManager.get_interface_from_args(
LLMProviderName("sciphi"),
)

# rename the other queries to `related_queries` until LLM output is re-factored.
completion["related_queries"] = completion.pop("other_queries")
generation_config = GenerationConfig(
model_name=llm_model_name,
max_tokens_to_sample=llm_max_tokens_to_sample,
temperature=llm_temperature,
top_p=llm_top_p,
)

completion = llm_interface.get_completion(
prompt, generation_config
).replace("</s>", "")

return completion

Expand Down
22 changes: 11 additions & 11 deletions docs/source/api/main.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ AgentSearch API Documentation

Welcome to the AgentSearch API documentation. Here, you'll find a detailed guide on how to use the different endpoints provided by the AgentSearch service. This API allows you to interact with the powerful functionalities of the AgentSearch codebase and associated AI models.

API Key and Signup
------------------

To access the SciPhi API, you need an API key. If you don't possess one, you can sign up `here <https://www.sciphi.ai/signup>`_. Ensure you include the API key in your request headers as shown in the examples.

Endpoint Overview
-----------------

1. **Search**: This endpoint allows you to fetch related search results for a given query. The results are powered by the AgentSearch framework and dataset.
2. **Completions**: This endpoint provides completions generated by the `Sensei`, SciPhi's expert search agent.
2. **Completions**: This endpoint provides completions generated by the `Sensei-7B <https://huggingface.co/SciPhi/Sensei-7B-V1/>` model, SciPhi's expert search agent.

Detailed Endpoint Descriptions
------------------------------
Expand Down Expand Up @@ -71,7 +76,7 @@ Search RAG Endpoint
~~~~~~~~~~~~~~~~~~~

- **URL**: ``/search_rag``
- **Method**: ``POST```
- **Method**: ``POST``
- **Description**: Retrieves a search RAG (Retrieval-Augmented Generation) response from the API.

**Request Body**:
Expand Down Expand Up @@ -111,7 +116,7 @@ A dictionary with the search response and related queries.
"search_results" : [{ ...see above... }]
}
LLM Completiongs Endpoint
LLM Completions Endpoint
~~~~~~~~~~~~~~~~~~~

If you would like to use the Sensei model for any purpose other than the provided Search RAG, perhaps with your own search context, then you may access the model directly.
Expand All @@ -125,14 +130,14 @@ SciPhi adheres to the API specification of OpenAI's API, allowing compatibility
.. code-block:: bash
export SEARCH_CONTEXT="N/A"
export PREFIX='{"response":'
export RESPONSE_PREFIX='{"response":'
curl https://api.sciphi.ai/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $SCIPHI_API_KEY" \
-d '{
"model": "SciPhi/Sensei-7B-V1",
"prompt": "### Instruction:\n\nQuery:\nWhat is the meaning of life?\n\nSearch Results:\n${SEARCH_CONTEXT}\n\nQuery:\nWhat is the meaning of life?\n### Response:\n${PREFIX}",
"prompt": "### Instruction:\n\nQuery:\nWhat is the meaning of life?\n\nSearch Results:\n${SEARCH_CONTEXT}\n\nQuery:\nWhat is the meaning of life?\n### Response:\n${RESPONSE_PREFIX}",
"temperature": 0.0
}'
Expand All @@ -159,9 +164,4 @@ SciPhi adheres to the API specification of OpenAI's API, allowing compatibility
"total_tokens":65,
"completion_tokens":16
}
}
API Key and Signup
------------------

To access the SciPhi API, you need an API key. If you don't possess one, you can sign up `here <https://www.sciphi.ai/signup>`_. Ensure you include the API key in your request headers as shown in the examples.
}
79 changes: 36 additions & 43 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Quickstart Guide for AgentSearch

`SciPhi API Key Signup <https://www.sciphi.ai/signup>`_

3. Code your own search agent workflow:
3. Call a pre-configured search agent endpoint:

.. code-block:: python
Expand All @@ -55,67 +55,60 @@ Quickstart Guide for AgentSearch
print(agent_summary)
# {'response': "The latest news encompasses ... and its consequences [2].", 'related_queries': ['Details on the...', ...], 'search_results' : [...]}
4. Standalone searches from the AgentSearch search engine are supported:
4. Standalone searches and from the AgentSearch search engine are supported:

.. code-block:: python
...
from agent_search import SciPhi
client = SciPhi()
# Perform a search
search_response = client.search(query='Quantum Field Theory', search_provider='agent-search')
print(search_response)
# [{ 'score': '.89', 'url': 'https://...', 'metadata': {...} }
Local Setup and Initialization
-------------------------------
Interested in standing up an instance of the open source search dataset that pairs with AgentSearch locally? Then follow the guide below.

.. warning::
This setup documentation is preliminary and not yet finalized. Please note that the setup process may change in the future.

Prerequisites
+++++++++++++

Ensure Docker and Postgres are installed:

- Docker: `Download from Docker's official website <https://www.docker.com/>`.
- Postgres: `Download from PostgreSQL's official website <https://www.postgresql.org/download/>`.

1. **Launch Postgres Database**:

.. code-block:: shell
sudo service postgresql start
2. **Populate Relational Database (Postgres)**:

.. code-block:: shell
5. Code your own custom search agent workflow:

python -m agent_search.scripts.populate_postgres_from_hf run
3. **Start Qdrant Service with Docker**:

.. code-block:: shell
docker run -p 6333:6333 -p 6334:6334 -v $(pwd)/qdrant_storage:/qdrant/storage:z qdrant/qdrant
4. **Populate Vector Database (Qdrant)**:

.. code-block:: shell
python -m agent_search.scripts.populate_qdrant_from_postgres run --delete_existing=True
.. code-block:: python
from agent_search import SciPhi
import json
5. **Run the Server**:
client = SciPhi()
.. code-block:: shell
# Specify instructions for the task
instruction = "Your task is to perform retrieval augmented generation (RAG) over the given query and search results. Return your answer in a json format that includes a summary of the search results and a list of related queries."
query = "What is Fermat's Last Theorem?"
# construct search context
search_response = client.search(query=query, search_provider='agent-search')
search_context = "\n\n".join(
f"{idx + 1}. Title: {item['title']}\nURL: {item['url']}\nText: {item['text']}"
for idx, item in enumerate(search_response)
).encode('utf-8')
# Prefix to enforce a JSON response
json_response_prefix = '{"summary":'
# Prepare a prompt
formatted_prompt = f"### Instruction:{instruction}\n\nQuery:\n{query}\n\nSearch Results:\n${search_context}\n\nQuery:\n{query}\n### Response:\n{json_response_prefix}",
python -m agent_search.app.server
# Generate a raw string completion with Sensei-7B-V1
completion = json_response_prefix + client.completion(formatted_prompt, llm_model_name="SciPhi/Sensei-7B-V1")
print(json.loads(completion))
# {
# "summary": "\nFermat's Last Theorem is a mathematical proposition first prop ... ",
# "other_queries": ["The role of elliptic curves in the proof of Fermat's Last Theorem", ...]
# }
Additional Notes
----------------

- Ensure all installation commands are executed from the root directory of the AgentSearch project.
- For support, join the `Discord community <https://discord.gg/mN4kWbsgRu>`

Documentation
-------------
Expand Down
Loading

0 comments on commit 01848d4

Please sign in to comment.