Skip to content

Commit 49b2e18

Browse files
committed
add feedback
1 parent 4921a4f commit 49b2e18

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

examples/feedback_example.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ def main():
1818
print(f"Error occurred: {e}")
1919

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

2527
if __name__ == "__main__":

examples/scrape_schema_example.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from pydantic import BaseModel
2+
from scrapegraphaiapisdk.scrape import scrape
3+
from dotenv import load_dotenv
4+
import os
5+
6+
# Load environment variables from .env file
7+
load_dotenv()
8+
9+
# Define a Pydantic schema
10+
class CompanyInfoSchema(BaseModel):
11+
company_name: str
12+
description: str
13+
main_products: list[str]
14+
15+
# Example usage
16+
api_key = os.getenv("SCRAPEGRAPH_API_KEY")
17+
url = "https://scrapegraphai.com/"
18+
prompt = "What does the company do?"
19+
20+
# Create an instance of the schema with initial values
21+
schema = CompanyInfoSchema(
22+
company_name="Example Company",
23+
description="An example company description.",
24+
main_products=["Product1", "Product2"]
25+
)
26+
27+
# Call the scrape function with the schema
28+
result = scrape(api_key=api_key, url=url, prompt=prompt, schema=schema)
29+
30+
print(result)

scrapegraphaiapisdk/feedback.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
import requests
99
import json
1010

11-
def feedback(api_key: str, feedback: str) -> str:
11+
def feedback(api_key: str, request_id: str, rating: int, feedback_text: str) -> str:
1212
"""Send feedback to the API.
1313
1414
Args:
1515
api_key (str): Your ScrapeGraph AI API key.
16-
feedback (str): The feedback message to send.
16+
request_id (str): The request ID associated with the feedback.
17+
rating (int): The rating score.
18+
feedback_text (str): The feedback message to send.
1719
1820
Returns:
1921
str: Response from the API in JSON format.
@@ -25,7 +27,11 @@ def feedback(api_key: str, feedback: str) -> str:
2527
"Content-Type": "application/json"
2628
}
2729

28-
feedback_data = {"feedback": feedback} # Prepare the feedback data
30+
feedback_data = {
31+
"request_id": request_id,
32+
"rating": rating,
33+
"feedback_text": feedback_text
34+
}
2935

3036
try:
3137
response = requests.post(endpoint, headers=headers, json=feedback_data)

scrapegraphaiapisdk/scrape.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ def scrape(api_key: str, url: str, prompt: str, schema: Optional[BaseModel] = No
3030
}
3131

3232
if schema:
33-
payload["schema"] = schema.model_json_schema()
33+
schema_json = schema.model_json_schema()
34+
payload["output_schema"] = {
35+
"description": schema_json.get("title", "Schema"),
36+
"name": schema_json.get("title", "Schema"),
37+
"properties": schema_json.get("properties", {}),
38+
"required": schema_json.get("required", [])
39+
}
3440

3541
try:
3642
response = requests.post(endpoint, headers=headers, json=payload)

tests/test_feedback.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import unittest
22
from unittest.mock import patch
33
from scrapegraphaiapisdk.feedback import feedback
4+
import requests
45

56
class TestFeedback(unittest.TestCase):
67

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

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

20-
2121
if __name__ == '__main__':
2222
unittest.main()

0 commit comments

Comments
 (0)