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

feat: add chat with pdf integration #244

Merged
merged 2 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions integrations/chat-with-pdf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Chat With PDF Agent

## Description
This code defines a Python module for an AI agent that interacts with a PDF using the pdf.ai API. The agent can receive a question and a URL of a PDF, upload the PDF if not already uploaded, and then retrieve an answer from the PDF.

## Authentication

To use the PDF.ai Summary API, you need to generate an API key. Follow the steps below to obtain your API key:

1. Visit the PDF.ai API Portal: [API Portal Link](https://pdf.ai/api-portal)
2. Sign in to your account or create a new one.
3. Navigate to the "API Keys" section in your account dashboard.
4. Click on "Generate New API Key."
5. Provide a name for your API key to easily identify its purpose.
6. Click "Generate Key" to create the API key.
7. Copy the generated API key and securely store it.

# Agent Secrets on Agentverse

1. Go to the Agentverse platform.
2. Navigate to the Agent Secrets section.
3. Create an agent and copy the code in it
4. Add a new secret with the key `API_KEY` and the value as your API KEY.

# Steps to Enroll an Agent as a Service on Agentverse

You can integrate into DeltaV your Agents created on your local computer, IoT devices, in the VMs, or agents created on Agentverse. The steps are the same.

Once your agents are run, the agent protocol manifests are uploaded to the Almanac contract in the form of protocol digests. After uploading the manifests, we take the agent addresses and enroll the agents as a service under the "Services" tab in Agentverse.

## Agent Validation on Agentverse Explorer
*Note: You can validate the procedure by searching for your agent's address on Agent Explorer, checking if the protocols have been uploaded successfully. If not, you need to wait for some time (1-2 minutes) until the protocols are uploaded successfully.*

## Create a Service Group

1. Start by creating a new service group on Agentverse.
2. Set up the service group as PRIVATE (you will only be able to see your own agents).
- If you set up your service group as Public, anyone will be able to see your agents.

**Service group has been created.**

## Create a Service

1. To register the agents as a service, input a concise title and description for the agent service.
2. Choose the service group for the agent service that you've created previously.
3. Fill in the agent address in the Agent field.
4. Set the task type to Task.

![Image](./image.png)

Now, your agents are enrolled as a service in Agentverse. You can manage and monitor them under the "Services" tab. Ensure that you follow the agent validation steps on Agent Explorer to confirm successful enrollment.
85 changes: 85 additions & 0 deletions integrations/chat-with-pdf/agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from ai_engine import UAgentResponse, UAgentResponseType

# Define the Chat With PDF Request model
class ChatWithPDFRequest(Model):
url: str
question: str

# Define the protocol for Chatting With PDF
chat_with_pdf_protocol = Protocol("Chat With PDF")

# Dictionary to store URL and corresponding docId
url_docId_map = {}

def upload_pdf_if_needed(url, ctx):
"""Uploads a PDF from a URL if it's not already uploaded, and returns the document ID."""
if url in url_docId_map:
ctx.logger.info(f"PDF is already uploaded")
return url_docId_map[url]

endpoint = "https://pdf.ai/api/v1/upload/url"
headers = {"X-API-Key": API_KEY}
payload = {"url": url, "isPrivate": False, "ocr": False}

try:
response = requests.post(endpoint, json=payload, headers=headers)
response.raise_for_status()
docId = response.json().get("docId")
url_docId_map[url] = docId # Store the docId for future reference
return docId
except Exception as e:
ctx.logger.info(f"Error during PDF upload: {e}")
return None

def chat_with_pdf(doc_id, question, ctx):
"""Sends a question to the PDF and returns the answer."""
endpoint = "https://pdf.ai/api/v1/chat"
headers = {"X-API-Key": API_KEY}
payload = {"docId": doc_id, "message": question, "save_chat": False, "language": "english", "use_gpt4": False}

try:
response = requests.post(endpoint, json=payload, headers=headers)
ctx.logger.info(f"Chat with PDF Response Status: {response.status_code}")
ctx.logger.info(f"Chat with PDF Response Body: {response.text}")
response.raise_for_status()
content = response.json().get("content", None)
return content
except Exception as e:
ctx.logger.info(f"Error during chat with PDF: {e}")
return None

@chat_with_pdf_protocol.on_message(model=ChatWithPDFRequest, replies=UAgentResponse)
async def on_message(ctx: Context, sender: str, msg: ChatWithPDFRequest):
ctx.logger.info(f"Received chat with PDF request from {sender}.")

try:
ctx.logger.info("Checking if PDF needs uploading")
doc_id = upload_pdf_if_needed(msg.url, ctx)
if not doc_id:
raise Exception("Failed to upload or retrieve PDF.")

ctx.logger.info("Chatting with the PDF")
answer = chat_with_pdf(doc_id, msg.question, ctx)
if not answer:
raise Exception("Failed to chat with PDF.")

await ctx.send(
sender,
UAgentResponse(
message=answer,
type=UAgentResponseType.FINAL
)
)

except Exception as exc:
ctx.logger.error(f"An error occurred: {exc}")
await ctx.send(
sender,
UAgentResponse(
message=f"Error: {exc}",
type=UAgentResponseType.ERROR
)
)

# Include the Chat With PDF protocol in your agent
agent.include(chat_with_pdf_protocol)
Binary file added integrations/chat-with-pdf/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions integrations/chat-with-pdf/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"title": "Chat with PDF",
"description": "This code defines for an AI agent that interacts with a PDF using the pdf.ai API. The agent can receive a question and a URL of a PDF, upload the PDF if not already uploaded, and then retrieve an answer from the PDF",
"categories": ["Text Summarization"],
"deltav": true
}
Loading