Implementing Retrieval-Augmented Generation (RAG) with ZCX can significantly enhance your application's ability to generate contextually relevant responses. ZCX provides an OpenAI-compatible API, enabling you to leverage Llamaindex and Langchain for RAG tasks efficiently. This post explores three minimal recipes for integrating RAG into your applications, focusing on cost-effectiveness and performance. Each example demonstrates how to set up RAG with ZCX, using Python code snippets to illustrate the process. By the end of this post, you'll have a clear understanding of how to implement RAG with ZCX, and you'll be able to choose the most suitable pattern for your project based on your specific needs and budget. Before diving into the code, ensure you have an API key from ZCX and have installed the necessary Python libraries for making HTTP requests and handling JSON data.
For a straightforward RAG implementation, Llamaindex offers a simple yet powerful way to integrate retrieval with generation. This pattern is ideal for applications that require quick setup and minimal configuration. Here's how you can set it up with ZCX:
import requests
import json
bearer_token = "YOUR_ZCX_BEARER_TOKEN"
headers = {"Authorization": f"Bearer {bearer_token}"}
# Example query for RAG
query = {"prompt": "What is the capital of France?", "retrieval": "true"}
response = requests.post("https://zcx.zctechnologies.org/v1/chat", headers=headers, data=json.dumps(query))
print(response.json())
For more advanced RAG use cases, Langchain provides a robust framework for chaining together retrieval and generation processes. This pattern is suitable for applications that need to handle complex data retrieval and generation tasks. Here's how you can implement it with ZCX:
from langchain import LLMChain, PromptTemplate
from langchain.chains import SequentialChain
from langchain.llms import ZCX
llm = ZCX(bearer_token="YOUR_ZCX_BEARER_TOKEN")
prompt_template_retrieval = PromptTemplate(input_variables=["query"], template="Retrieve information about {query}")
prompt_template_generation = PromptTemplate(input_variables=["retrieved_info"], template="Generate a response based on {retrieved_info}")
retrieval_chain = LLMChain(llm=llm, prompt=prompt_template_retrieval)
generation_chain = LLMChain(llm=llm, prompt=prompt_template_generation)
sequential_chain = SequentialChain(chains=[retrieval_chain, generation_chain], input_variables=["query"], output_variables=["retrieved_info", "response"])
output = sequential_chain({"query": "What is the capital of France?"})
print(output)
For developers who prefer to have full control over the RAG process, directly interacting with the ZCX API allows for customization and optimization. This pattern is best for applications that require fine-tuning of retrieval and generation parameters. Here's an example of how to implement this pattern:
import requests
import json
bearer_token = "YOUR_ZCX_BEARER_TOKEN"
headers = {"Authorization": f"Bearer {bearer_token}"}
# Custom RAG setup
query = {"prompt": "What is the capital of France?", "retrieval": "true", "retrieval_params": {"source": "wikipedia"}}
response = requests.post("https://zcx.zctechnologies.org/v1/chat", headers=headers, data=json.dumps(query))
print(response.json())
Implementing RAG with ZCX can offer significant advantages in terms of cost and performance. With ZCX, you can choose from different subscription plans to fit your project's budget and scale. For instance, the Starter plan at $99/mo provides 1.5M tokens, while the Business plan at $1999/mo offers 60.0M tokens. Each pattern presented here is designed to help you get started quickly and efficiently. To learn more about our plans and pricing, visit https://zcx.zctechnologies.org#plans.