Skip to content

Commit

Permalink
add feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
VinciGit00 committed Nov 2, 2024
1 parent 4921a4f commit 49b2e18
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
4 changes: 3 additions & 1 deletion examples/feedback_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ def main():
print(f"Error occurred: {e}")

# Example usage of feedback function
request_id = "3fa85f64-5717-4562-b3fc-2c963f66afa6"
rating = 5
feedback_message = "This is a test feedback message."
feedback_response = feedback(api_key, feedback_message) # Call the feedback function
feedback_response = feedback(api_key, request_id, rating, feedback_message) # Call the feedback function
print(f"Feedback Response: {feedback_response}") # Print the response

if __name__ == "__main__":
Expand Down
30 changes: 30 additions & 0 deletions examples/scrape_schema_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from pydantic import BaseModel
from scrapegraphaiapisdk.scrape import scrape
from dotenv import load_dotenv
import os

# Load environment variables from .env file
load_dotenv()

# Define a Pydantic schema
class CompanyInfoSchema(BaseModel):
company_name: str
description: str
main_products: list[str]

# Example usage
api_key = os.getenv("SCRAPEGRAPH_API_KEY")
url = "https://scrapegraphai.com/"
prompt = "What does the company do?"

# Create an instance of the schema with initial values
schema = CompanyInfoSchema(
company_name="Example Company",
description="An example company description.",
main_products=["Product1", "Product2"]
)

# Call the scrape function with the schema
result = scrape(api_key=api_key, url=url, prompt=prompt, schema=schema)

print(result)
12 changes: 9 additions & 3 deletions scrapegraphaiapisdk/feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import requests
import json

def feedback(api_key: str, feedback: str) -> str:
def feedback(api_key: str, request_id: str, rating: int, feedback_text: str) -> str:
"""Send feedback to the API.
Args:
api_key (str): Your ScrapeGraph AI API key.
feedback (str): The feedback message to send.
request_id (str): The request ID associated with the feedback.
rating (int): The rating score.
feedback_text (str): The feedback message to send.
Returns:
str: Response from the API in JSON format.
Expand All @@ -25,7 +27,11 @@ def feedback(api_key: str, feedback: str) -> str:
"Content-Type": "application/json"
}

feedback_data = {"feedback": feedback} # Prepare the feedback data
feedback_data = {
"request_id": request_id,
"rating": rating,
"feedback_text": feedback_text
}

try:
response = requests.post(endpoint, headers=headers, json=feedback_data)
Expand Down
8 changes: 7 additions & 1 deletion scrapegraphaiapisdk/scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ def scrape(api_key: str, url: str, prompt: str, schema: Optional[BaseModel] = No
}

if schema:
payload["schema"] = schema.model_json_schema()
schema_json = schema.model_json_schema()
payload["output_schema"] = {
"description": schema_json.get("title", "Schema"),
"name": schema_json.get("title", "Schema"),
"properties": schema_json.get("properties", {}),
"required": schema_json.get("required", [])
}

try:
response = requests.post(endpoint, headers=headers, json=payload)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_feedback.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import unittest
from unittest.mock import patch
from scrapegraphaiapisdk.feedback import feedback
import requests

class TestFeedback(unittest.TestCase):

@patch('scrapegraphaiapisdk.feedback.requests.post')
def test_feedback_success(self, mock_post):
mock_post.return_value.status_code = 200
mock_post.return_value.text = '{"status": "success"}'
response = feedback("test_api_key", "Great service!")
response = feedback("test_api_key", "3fa85f64-5717-4562-b3fc-2c963f66afa6", 5, "Great service!")
self.assertEqual(response, '{"status": "success"}')

@patch('scrapegraphaiapisdk.feedback.requests.post')
def test_feedback_http_error(self, mock_post):
mock_post.side_effect = requests.exceptions.HTTPError
response = feedback("test_api_key", "Great service!")
response = feedback("test_api_key", "3fa85f64-5717-4562-b3fc-2c963f66afa6", 5, "Great service!")
self.assertIn("HTTP error occurred", response)


if __name__ == '__main__':
unittest.main()

0 comments on commit 49b2e18

Please sign in to comment.