-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Implement JS entity extraction from text #999
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
/* Markdown (render) | ||
# Gemini API: Entity extraction | ||
|
||
Use Gemini API to speed up some of your tasks, such as searching through text to extract needed information. Entity extraction with a Gemini model is a simple query, and you can ask it to retrieve its answer in the form that you prefer. | ||
|
||
This notebook shows how to extract entities into a list. | ||
|
||
## Setup | ||
### Install SDK and set-up the client | ||
|
||
### API Key Configuration | ||
|
||
To ensure security, avoid hardcoding the API key in frontend code. Instead, set it as an environment variable on the server or local machine. | ||
|
||
When using the Gemini API client libraries, the key will be automatically detected if set as either `GEMINI_API_KEY` or `GOOGLE_API_KEY`. If both are set, `GOOGLE_API_KEY` takes precedence. | ||
|
||
For instructions on setting environment variables across different operating systems, refer to the official documentation: [Set API Key as Environment Variable](https://ai.google.dev/gemini-api/docs/api-key#set-api-env-var) | ||
|
||
In code, the key can then be accessed as: | ||
|
||
```js | ||
ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); | ||
``` | ||
*/ | ||
|
||
// [CODE STARTS] | ||
module = await import("https://esm.sh/@google/genai@1.4.0"); | ||
GoogleGenAI = module.GoogleGenAI; | ||
ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY }); | ||
|
||
MODEL_ID = "gemini-2.5-flash" // ["gemini-2.5-flash-lite", "gemini-2.5-flash", "gemini-2.5-pro"] | ||
// [CODE ENDS] | ||
|
||
/* Markdown (render) | ||
### Extracting few entities at once | ||
|
||
This block of text is about possible ways to travel from the airport to the Colosseum. | ||
|
||
Let's extract all street names and proposed forms of transportation from it. | ||
*/ | ||
|
||
// [CODE STARTS] | ||
directions = ` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
To reach the Colosseum from Rome's Fiumicino Airport (FCO), | ||
your options are diverse. Take the Leonardo Express train from FCO | ||
to Termini Station, then hop on metro line A towards Battistini and | ||
alight at Colosseo station. | ||
Alternatively, hop on a direct bus, like the Terravision shuttle, from | ||
FCO to Termini, then walk a short distance to the Colosseum on | ||
Via dei Fori Imperiali. | ||
If you prefer a taxi, simply hail one at the airport and ask to be taken | ||
to the Colosseum. The taxi will likely take you through Via del Corso and | ||
Via dei Fori Imperiali. | ||
A private transfer service offers a direct ride from FCO to the Colosseum, | ||
bypassing the hustle of public transport. | ||
If you're feeling adventurous, consider taking the train from | ||
FCO to Ostiense station, then walking through the charming | ||
Trastevere neighborhood, crossing Ponte Palatino to reach the Colosseum, | ||
passing by the Tiber River and Via della Lungara. | ||
Remember to validate your tickets on the metro and buses, | ||
and be mindful of pickpockets, especially in crowded areas. | ||
No matter which route you choose, you're sure to be awed by the | ||
grandeur of the Colosseum. | ||
`; | ||
// [CODE ENDS] | ||
|
||
/* Markdown (render) | ||
You will use Gemini Flash model for fast responses. | ||
*/ | ||
|
||
// [CODE STARTS] | ||
directionsPrompt = ` | ||
From the given text, extract the following entities and return a list of them. | ||
Entities to extract: street name, form of transport. | ||
Text: ${directions} | ||
Street = [] | ||
Transport = [] | ||
`; | ||
|
||
response = await ai.models.generateContent({ | ||
model: MODEL_ID, | ||
contents: [directionsPrompt], | ||
}); | ||
Comment on lines
+72
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variables
|
||
|
||
console.log(response.text); | ||
|
||
// [CODE ENDS] | ||
|
||
/* Output Sample | ||
|
||
Street = [ | ||
"Via dei Fori Imperiali", | ||
"Via del Corso", | ||
"Via della Lungara" | ||
] | ||
Transport = [ | ||
"Leonardo Express train", | ||
"metro line A", | ||
"bus", | ||
"Terravision shuttle", | ||
"walking", | ||
"taxi", | ||
"private transfer service", | ||
"public transport", | ||
"train", | ||
"metro" | ||
] | ||
|
||
*/ | ||
|
||
/* Markdown (render) | ||
You can modify the form of the answer for your extracted entities even more: | ||
*/ | ||
|
||
// [CODE STARTS] | ||
directionsListPrompt = ` | ||
From the given text, extract the following entities and | ||
return a list of them. | ||
Entities to extract: street name, form of transport. | ||
Text: ${directions} | ||
Return your answer as two lists: | ||
Street = [street names] | ||
Transport = [forms of transport] | ||
`; | ||
|
||
response = await ai.models.generateContent({ | ||
model: MODEL_ID, | ||
contents: [directionsListPrompt], | ||
}); | ||
Comment on lines
+116
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variables
|
||
|
||
console.log(response.text); | ||
|
||
// [CODE ENDS] | ||
|
||
/* Output Sample | ||
|
||
Street = ['Via dei Fori Imperiali', 'Via del Corso', 'Via della Lungara'] | ||
Transport = ['Leonardo Express train', 'metro line A', 'bus', 'Terravision shuttle', 'taxi', 'private transfer service', 'train', 'walking', 'metro'] | ||
|
||
*/ | ||
|
||
/* Markdown (render) | ||
### Numbers | ||
|
||
Try entity extraction of phone numbers | ||
*/ | ||
|
||
// [CODE STARTS] | ||
customerServiceEmail = ` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Hello, | ||
Thank you for reaching out to our customer support team regarding your | ||
recent purchase of our premium subscription service. | ||
Your activation code has been sent to +87 668 098 344 | ||
Additionally, if you require immediate assistance, feel free to contact us | ||
directly at +1 (800) 555-1234. | ||
Our team is available Monday through Friday from 9:00 AM to 5:00 PM PST. | ||
For after-hours support, please call our | ||
dedicated emergency line at +87 455 555 678. | ||
Thanks for your business and look forward to resolving any issues | ||
you may encounter promptly. | ||
Thank you. | ||
`; | ||
|
||
// [CODE ENDS] | ||
|
||
// [CODE STARTS] | ||
phonePrompt = ` | ||
From the given text, extract the following entities and return a list of them. | ||
Entities to extract: phone numbers. | ||
Text: ${customerServiceEmail} | ||
Return your answer in a list: | ||
`; | ||
|
||
response = await ai.models.generateContent({ | ||
model: MODEL_ID, | ||
contents: [phonePrompt], | ||
}); | ||
Comment on lines
+167
to
+177
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
|
||
console.log(response.text); | ||
|
||
// [CODE ENDS] | ||
|
||
/* Output Sample | ||
|
||
```json | ||
[ | ||
"+87 668 098 344", | ||
"+1 (800) 555-1234", | ||
"+87 455 555 678" | ||
] | ||
``` | ||
|
||
*/ | ||
|
||
/* Markdown (render) | ||
### URLs | ||
|
||
|
||
Try entity extraction of URLs and get response as a clickable link. | ||
*/ | ||
|
||
// [CODE STARTS] | ||
urlText = ` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Gemini API billing FAQs | ||
|
||
This page provides answers to frequently asked questions about billing | ||
for the Gemini API. For pricing information, see the pricing page | ||
https://ai.google.dev/pricing. | ||
For legal terms, see the terms of service | ||
https://ai.google.dev/gemini-api/terms#paid-services. | ||
|
||
What am I billed for? | ||
Gemini API pricing is based on total token count, with different prices | ||
for input tokens and output tokens. For pricing information, | ||
see the pricing page https://ai.google.dev/pricing. | ||
|
||
Where can I view my quota? | ||
You can view your quota and system limits in the Google Cloud console | ||
https://console.cloud.google.com/apis/api/generativelanguage.googleapis.com/quotas. | ||
|
||
Is GetTokens billed? | ||
Requests to the GetTokens API are not billed, | ||
and they don't count against inference quota. | ||
`; | ||
|
||
// [CODE ENDS] | ||
|
||
// [CODE STARTS] | ||
urlPrompt = ` | ||
From the given text, extract the following entities and return a list of them. | ||
Entities to extract: URLs. | ||
Text: ${urlText} | ||
Do not duplicate entities. | ||
Return your answer in a markdown format: | ||
`; | ||
|
||
response = await ai.models.generateContent({ | ||
model: MODEL_ID, | ||
contents: [urlPrompt], | ||
}); | ||
Comment on lines
+229
to
+240
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
|
||
console.log(response.text); | ||
|
||
// [CODE ENDS] | ||
|
||
/* Output Sample | ||
|
||
- `https://ai.google.dev/pricing` | ||
- `https://ai.google.dev/gemini-api/terms#paid-services` | ||
- `https://console.cloud.google.com/apis/api/generativelanguage.googleapis.com/quotas` | ||
|
||
*/ |
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.
In JavaScript, it's a best practice to always declare variables with
const
,let
, orvar
to avoid creating implicit globals. This improves code clarity and prevents potential naming conflicts. For the SDK import, you can use object destructuring for a more concise syntax.