|
1 | 1 | # Microsoft.Extensions.AI Integration |
2 | 2 |
|
3 | | -ScrapeGraphAI provides `AIFunction` tools for seamless integration with any `IChatClient` via [Microsoft.Extensions.AI](https://devblogs.microsoft.com/dotnet/introducing-microsoft-extensions-ai-preview/). |
| 3 | +!!! tip "Cross-SDK comparison" |
| 4 | + See the [centralized MEAI documentation](https://tryagi.github.io/docs/meai/) for feature matrices and comparisons across all tryAGI SDKs. |
| 5 | + |
| 6 | +The ScrapeGraphAI SDK provides `AIFunction` tool wrappers compatible with [Microsoft.Extensions.AI](https://learn.microsoft.com/en-us/dotnet/ai/microsoft-extensions-ai). These tools can be used with any `IChatClient` to give AI models access to ScrapeGraphAI's smart web scraping, AI-powered search, markdown conversion, sitemap extraction, and credit management. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +```bash |
| 11 | +dotnet add package ScrapeGraphAI |
| 12 | +``` |
4 | 13 |
|
5 | 14 | ## Available Tools |
6 | 15 |
|
7 | | -| Tool | Method | Description | |
8 | | -|------|--------|-------------| |
9 | | -| SmartScraper | `AsSmartScraperTool()` | Extract structured data from a URL using a natural language prompt | |
10 | | -| SearchScraper | `AsSearchScraperTool()` | AI-powered web search with structured results | |
11 | | -| Markdownify | `AsMarkdownifyTool()` | Convert a webpage to clean markdown | |
12 | | -| GetCredits | `AsGetCreditsTool()` | Check remaining API credit balance | |
13 | | -| GetSitemap | `AsGetSitemapTool()` | Extract all URLs from a website's sitemap | |
| 16 | +| Method | Tool Name | Description | |
| 17 | +|--------|-----------|-------------| |
| 18 | +| `AsSmartScraperTool()` | `SmartScraper` | Extract structured data from a URL using natural language prompts | |
| 19 | +| `AsSearchScraperTool()` | `SearchScraper` | AI-powered web search with structured results | |
| 20 | +| `AsMarkdownifyTool()` | `Markdownify` | Convert a webpage to clean, readable markdown | |
| 21 | +| `AsGetCreditsTool()` | `GetCredits` | Check remaining API credits and total usage | |
| 22 | +| `AsGetSitemapTool()` | `GetSitemap` | Extract all URLs from a website's sitemap | |
14 | 23 |
|
15 | | -## Usage Example |
| 24 | +## Usage |
16 | 25 |
|
17 | 26 | ```csharp |
18 | | -using Microsoft.Extensions.AI; |
19 | 27 | using ScrapeGraphAI; |
| 28 | +using Microsoft.Extensions.AI; |
20 | 29 |
|
21 | | -// Create the ScrapeGraphAI client |
22 | | -using var scraper = new ScrapeGraphAIClient(apiKey); |
| 30 | +var scrapeClient = new ScrapeGraphAIClient( |
| 31 | + apiKey: Environment.GetEnvironmentVariable("SGAI_API_KEY")!); |
23 | 32 |
|
24 | | -// Create tools |
25 | | -var tools = new[] |
| 33 | +var options = new ChatOptions |
26 | 34 | { |
27 | | - scraper.AsSmartScraperTool(), |
28 | | - scraper.AsSearchScraperTool(), |
29 | | - scraper.AsMarkdownifyTool(), |
| 35 | + Tools = |
| 36 | + [ |
| 37 | + scrapeClient.AsSmartScraperTool(), |
| 38 | + scrapeClient.AsSearchScraperTool(), |
| 39 | + scrapeClient.AsMarkdownifyTool(), |
| 40 | + scrapeClient.AsGetCreditsTool(), |
| 41 | + scrapeClient.AsGetSitemapTool(), |
| 42 | + ], |
30 | 43 | }; |
31 | 44 |
|
32 | | -// Use with any IChatClient |
33 | 45 | IChatClient chatClient = /* your chat client */; |
34 | | -var response = await chatClient.GetResponseAsync( |
35 | | - "Extract the main heading from https://example.com", |
36 | | - new ChatOptions { Tools = tools }); |
37 | | -``` |
38 | | - |
39 | | -## Tool Details |
40 | | - |
41 | | -### SmartScraper |
42 | 46 |
|
43 | | -Extracts structured data from any website using AI. Requires a URL and a natural language prompt describing what to extract. |
44 | | - |
45 | | -```csharp |
46 | | -var tool = client.AsSmartScraperTool(); |
47 | | -``` |
48 | | - |
49 | | -### SearchScraper |
50 | | - |
51 | | -Performs an AI-powered web search and returns structured results. Optionally configure the number of results (3-20). |
52 | | - |
53 | | -```csharp |
54 | | -var tool = client.AsSearchScraperTool(numResults: 5); |
55 | | -``` |
56 | | - |
57 | | -### Markdownify |
58 | | - |
59 | | -Converts a webpage to clean, readable markdown format. |
60 | | - |
61 | | -```csharp |
62 | | -var tool = client.AsMarkdownifyTool(); |
63 | | -``` |
64 | | - |
65 | | -### GetCredits |
66 | | - |
67 | | -Retrieves the current credit balance and total credits used. |
68 | | - |
69 | | -```csharp |
70 | | -var tool = client.AsGetCreditsTool(); |
71 | | -``` |
72 | | - |
73 | | -### GetSitemap |
74 | | - |
75 | | -Extracts all URLs from a website's sitemap. |
| 47 | +var messages = new List<ChatMessage> |
| 48 | +{ |
| 49 | + new(ChatRole.User, "Extract the pricing information from https://example.com/pricing"), |
| 50 | +}; |
76 | 51 |
|
77 | | -```csharp |
78 | | -var tool = client.AsGetSitemapTool(); |
| 52 | +while (true) |
| 53 | +{ |
| 54 | + var response = await chatClient.GetResponseAsync(messages, options); |
| 55 | + messages.AddRange(response.ToChatMessages()); |
| 56 | + |
| 57 | + if (response.FinishReason == ChatFinishReason.ToolCalls) |
| 58 | + { |
| 59 | + var results = await response.CallToolsAsync(options); |
| 60 | + messages.AddRange(results); |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + Console.WriteLine(response.Text); |
| 65 | + break; |
| 66 | +} |
79 | 67 | ``` |
0 commit comments