-
Notifications
You must be signed in to change notification settings - Fork 28
Replace gRPC URL resolver with Gemini urlContext implementation #377
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
Draft
MrOrz
wants to merge
9
commits into
master
Choose a base branch
from
claude/issue-373-20250812-0421
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
402be33
Replace gRPC URL resolver with Gemini urlContext implementation
claude[bot] 5194dc4
Fix Gemini URL scraper based on review feedback
claude[bot] 1402d39
fix: lint
MrOrz 125194b
fix: improve error logging in URL scraper experiment
MrOrz 2faeb47
fix: format
MrOrz ef1ca5f
fix: update Gemini URL scraper prompt to extract exact content instea…
claude[bot] 9706414
fix: show full summary logging in URL scraper experiments
MrOrz 9d5f697
fix: refine summary extraction criteria in Gemini URL scraper
MrOrz c24425b
feat: add YouTube video transcription function using Gemini model
MrOrz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/** | ||
* Script to test URL scraping using Gemini urlContext tool. | ||
* | ||
* Usage: | ||
* npx tsx src/scripts/experimentUrlScraper.ts \ | ||
* --runName "url-scraper-experiment-1" \ | ||
* [--urls "https://example.com,https://another-site.com"] \ | ||
* [--single "https://single-test-url.com"] | ||
* | ||
* Required args: | ||
* --runName: Name to identify this experiment run in Langfuse | ||
* | ||
* Optional args: | ||
* --urls: Comma-separated list of URLs to test | ||
* --single: Single URL to test (alternative to --urls) | ||
*/ | ||
import 'dotenv/config'; | ||
import yargs from 'yargs'; | ||
|
||
import scrapeUrlsWithGemini from '../util/geminiUrlScraper.js'; | ||
import langfuse from 'util/langfuse'; | ||
|
||
// Default test URLs for experimentation | ||
const DEFAULT_TEST_URLS = [ | ||
'https://www.cofacts.tw', | ||
'https://github.com/cofacts/rumors-api', | ||
'https://www.taiwannews.com.tw/en/news/5023456', | ||
]; | ||
|
||
async function main({ | ||
urls, | ||
single, | ||
runName, | ||
}: { | ||
urls?: string; | ||
single?: string; | ||
runName: string; | ||
}) { | ||
let testUrls: string[]; | ||
|
||
if (single) { | ||
testUrls = [single]; | ||
} else if (urls) { | ||
testUrls = urls | ||
.split(',') | ||
.map((url) => url.trim()) | ||
.filter(Boolean); | ||
} else { | ||
testUrls = DEFAULT_TEST_URLS; | ||
console.info('No URLs specified, using default test URLs:', testUrls); | ||
} | ||
|
||
if (testUrls.length === 0) { | ||
console.info('No valid URLs to process. Exiting.'); | ||
return; | ||
} | ||
console.info(`Testing URL scraping with ${testUrls.length} URLs`); | ||
|
||
const trace = langfuse.trace({ | ||
name: `URL Scraper Experiment: ${runName}`, | ||
input: testUrls, | ||
metadata: { | ||
experimentType: 'url-scraping', | ||
tool: 'gemini-urlcontext', | ||
urlCount: testUrls.length, | ||
}, | ||
}); | ||
|
||
try { | ||
console.info('Starting URL scraping...'); | ||
const startTime = Date.now(); | ||
|
||
const results = await scrapeUrlsWithGemini(testUrls); | ||
|
||
const endTime = Date.now(); | ||
const duration = endTime - startTime; | ||
|
||
console.info(`\n=== RESULTS ===`); | ||
console.info(`Processed ${results.length} URLs in ${duration}ms`); | ||
console.info( | ||
`Average time per URL: ${Math.round(duration / results.length)}ms\n` | ||
); | ||
|
||
results.forEach((result, index) => { | ||
console.info(`--- URL ${index + 1}: ${result.url} ---`); | ||
console.info(`Status: ${result.status}`); | ||
|
||
if (result.status === 'SUCCESS') { | ||
console.info(`Title: ${result.title || 'N/A'}`); | ||
console.info( | ||
`Summary: ${ | ||
result.summary ? result.summary.substring(0, 200) + '...' : 'N/A' | ||
}` | ||
); | ||
console.info(`Top Image: ${result.topImageUrl || 'N/A'}`); | ||
} else { | ||
console.info(`Error: ${result.error}`); | ||
} | ||
console.info(''); | ||
}); | ||
|
||
// Count success/failure rates | ||
const successCount = results.filter((r) => r.status === 'SUCCESS').length; | ||
const errorCount = results.filter((r) => r.status === 'ERROR').length; | ||
|
||
console.info('=== SUMMARY ==='); | ||
console.info( | ||
`Success rate: ${successCount}/${results.length} (${Math.round( | ||
(successCount / results.length) * 100 | ||
)}%)` | ||
); | ||
console.info( | ||
`Error rate: ${errorCount}/${results.length} (${Math.round( | ||
(errorCount / results.length) * 100 | ||
)}%)` | ||
); | ||
console.info(`Total processing time: ${duration}ms`); | ||
|
||
// Record results in Langfuse | ||
trace.update({ | ||
output: results, | ||
metadata: { | ||
successCount, | ||
errorCount, | ||
totalDuration: duration, | ||
averageDurationPerUrl: Math.round(duration / results.length), | ||
}, | ||
}); | ||
|
||
// Score the experiment based on success rate | ||
trace.score({ | ||
name: 'success-rate', | ||
value: successCount / results.length, | ||
comment: `${successCount} successful out of ${results.length} URLs`, | ||
}); | ||
|
||
// Score based on average processing time (lower is better, normalize to 0-1) | ||
const avgTimePerUrl = duration / results.length; | ||
const timeScore = Math.max(0, 1 - avgTimePerUrl / 10000); // Penalize if >10s per URL | ||
trace.score({ | ||
name: 'processing-speed', | ||
value: timeScore, | ||
comment: `Average ${Math.round(avgTimePerUrl)}ms per URL`, | ||
}); | ||
} catch (error) { | ||
console.error('Experiment failed:', error); | ||
trace.update({ | ||
output: { error }, | ||
}); | ||
trace.score({ | ||
name: 'success-rate', | ||
value: 0, | ||
comment: `Experiment failed: ${error}`, | ||
}); | ||
} | ||
|
||
await langfuse.flushAsync(); | ||
} | ||
|
||
/* istanbul ignore if */ | ||
if (require.main === module) { | ||
const argv = yargs | ||
.options({ | ||
runName: { | ||
description: 'Name to identify this experiment run in Langfuse', | ||
type: 'string', | ||
demandOption: true, | ||
}, | ||
urls: { | ||
description: 'Comma-separated list of URLs to test', | ||
type: 'string', | ||
}, | ||
single: { | ||
description: 'Single URL to test (alternative to --urls)', | ||
type: 'string', | ||
}, | ||
}) | ||
.help('help') | ||
.parseSync(); | ||
|
||
main(argv).catch(console.error); | ||
} | ||
|
||
export default main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Mock implementation for Gemini URL scraper | ||
const scrapeUrlsWithGemini = jest.fn(); | ||
|
||
export default scrapeUrlsWithGemini; | ||
Check failure on line 4 in src/util/__mocks__/geminiUrlScraper.js
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the
urls
argument results in an empty list of URLs after trimming and filtering, the script could encounter division-by-zero errors later when calculating statistics. It's safer to add a check to exit early iftestUrls
is empty.