Hi @nathaniel, I have created an action that talks to the Pinecone assistant. I actually need this for a client that wants to use Pinecone.
BUT…I’m having problems printing any information other than the “content”. I want to print the citation and the file URL.
Any idea?
This is what Pinecone returns:
{
"finish_reason": "stop",
"message": {
"role": "\"assistant\"",
"content": "The Melt Flow Rate (MFR) for SUPEER™ 7358A, which is a measure of its melt index (MI), is 3.5 g/10 min at 190 °C and 2.16 kg load ."
},
"id": "00000000000000003905f6d65099910c",
"model": "gpt-4o-2024-05-13",
"usage": {
"prompt_tokens": 17932,
"completion_tokens": 63,
"total_tokens": 17995
},
"citations": [
{
"position": 133,
"references": [
{
"file": {
"status": "Available",
"id": "64c73bf7-eb1a-47ba-bb9b-174c259fea7c",
"name": "SUPEER™ mLLDPE_7358A_Americas_Technical_Data_Sheet.pdf",
"size": 109892,
"metadata": null,
"updated_on": "2024-11-27T22:58:03.698981058Z",
"created_on": "2024-11-27T22:57:34.371168437Z",
"percent_done": 1.0,
"signed_url": "https://storage.googleapis.com/knowledge-prod-files/72d194e1-8a54-4684-bf8b-386c64f00cb7%2Fb16d32ca-d0a8-4328-91b5-798b60bcb853%2F64c73bf7-eb1a-47ba-bb9b-174c259fea7c.pdf?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=ke-prod-1%40pc-knowledge-prod.iam.gserviceaccount.com%2F20241128%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20241128T024002Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&response-content-disposition=inline&response-content-type=application%2Fpdf&X-Goog-Signature=8c61bc6be80a45476a8aa7abf20b40f7ea98c0eaf2be4a8ac1949d98c4a3ebe1df77301aee5bac57f6d7a3342ad4dfedf75ece562004349d2c3f491d604ad2ce30a0d70ebecb7858177f03bf60ebf5519e11df6ce57f3e6ad74c71549909fbac0650838c686acc1deb154879b42a5806e7f110ef4f999177a2ef975e4f63e588f976c91f99bacd6aca910b2bdf39d89093a715e31c280356c79809e741c21b121b6dfb7be0fa48ecd560ed698cd24da53918e13a0014d9f34dab60ad55b3e41dbd455678f0b5aef3d057becb600ec7e14985403a7fdbb7c8c162643afb556e297dd97ec442c6321b8a7854c7ac3fd14a0a4437b9efe389107b869cc65b1b4dfe"
},
"pages": [
2,
1
]
}
]
}
]
}
This is my action (for testing purposes I’m not using environment variables). It works but I can’t get Pickaxe to return any other info other than the content:
import os
import requests
import json
def pinecone_assistant(question: str):
"""
Send the question to the Pinecone assistant to retrieve an answer
Args:
question (string): User's question
"""
# Insert your PYTHON code below. You can access environment variables using os.environ[].
# Currently, only the requests library is supported, but more libraries will be available soon.
# Use print statements or return values to display results to the user.
# If you save a png, pdf, csv, jpg, webp, gif, or html file in the root directory, it will be automatically displayed to the user.
# You do not have to call this function as the bot will automatically call and fill in the parameters.
# Pinecone Assistant API endpoint
url = "https://prod-1-data.ke.pinecone.io/assistant/chat/test-xxx"
# Payload containing the user's question
payload = {
"messages": [
{
"role": "user",
"content": question
}
],
"stream": False,
"model": "gpt-4o"
}
# Headers including the API key for authentication
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer pcsk_6T6btv_LEyA7BxeAdzErJoogVeVKRAhS94hMXFmAKFTiYJSptMBK9KiWo2ri2aqtXXXXXXXXX'
}
try:
# Make the POST request
response = requests.post(url, headers=headers, json=payload, timeout=10)
# Parse the JSON response
response_data = response.json()
# Log the full response (for debugging)
print("Full Response JSON:", json.dumps(response_data, indent=2))
# Extract assistant reply
assistant_reply = response_data.get("message", {}).get("content", "No response received.")
# Extract file details (assuming first citation exists)
citations = response_data.get("citations", [])
if citations:
first_citation = citations[0].get("references", [])[0].get("file", {})
file_name = first_citation.get("name", "No file name available")
file_url = first_citation.get("signed_url", "No file URL available")
else:
file_name = "No citations available"
file_url = "No file URL available"
# Return all details as plain text
result = (
f"Assistant Reply:\n{assistant_reply}\n\n"
f"File Name: {file_name}\n"
f"File URL: {file_url}"
)
return result
except Exception as e:
# Return error information
return f"An error occurred: {e}"
# Example Usage
user_question = "What is the MI for SABIC Supeer 7358A?"
assistant_response = pinecone_assistant(user_question)
if assistant_response:
print(assistant_response)
Thank you
@katrin_birkholz