Kong and Bard: shaking hands with A(P)I
Chatting is not the only way to access Google Bard AI.
There is a way to interact with it via API.
Here are some of the things you can do with the Bard API:
Bard is currently in beta, and the API is not yet publicly available. However, if you are interested in using the API, you can follow my little tutorial provided in this article.
Here are the steps on how to interface with Bard via API:
Here is an code snippet example of how to call the API:
import google.auth
import generative_language_api
# Create a GoogleCredentials object from the JSON key file.
credentials = google.auth.load_credentials_from_file("my_key.json")
# Create a GenerativeLanguageApi object.
api = generative_language_api.GenerativeLanguageApi(credentials)
# Call the generate_text method and pass in the text you want to generate.
generated_text = api.generate_text("What is the meaning of life?")
# Print the generated text.
print(generated_text.text)
The output of the above code will be:
The meaning of life is to find your gift. The purpose of life is to give it away.
You can also use the Kong Gateway to call the API. Here are the steps on how to do this:
Here is an example of a request that you can send to the Kong Gateway:
curl -X POST \
?-H "Authorization: Bearer <your_access_token>" \
?-H "Content-Type: application/json" \
?-d '{
??"text": "What is the meaning of life?"
?}' \
?https://localhost:8000/bard
The output of the above request will be:
{
?"text": "The meaning of life is to find your gift. The purpose of life is to give it away."
}
If you are new to Kong, those are the steps in more detail:
领英推荐
You can create a Kong Gateway instance using the Kong Docker image. To do this, you will need to have Docker installed on your computer. Once you have Docker installed, you can create a Kong Gateway instance by running the following command:
docker run -d -p 8000:8000 kong
This will create a Kong Gateway instance that is listening on port 8000.
2. Create a new route and select the Google Bard API as the upstream service.
Once you have created a Kong Gateway instance, you can create a new route. To do this, you will need to use the Kong Admin API. You can access the Kong Admin API by opening a web browser and navigating to https://localhost:8000/admin
Once you have opened the Kong Admin API, you can create a new route by clicking on the "Routes" tab and then clicking on the "Create Route" button.
In the "Create Route" dialog, you will need to provide the following information:
* **Name:** The name of the route.
* **Path:** The path that the route will match.
* **Upstream:** The upstream service that the route will point to. In this case, the upstream service is the `Google Bard API`.
3. Configure the route with the following parameters:
Once you have created the route, you will need to configure it with the following parameters:
* **Authorization:** The value of the `Authorization` header that you will use to authenticate with the API. The value of the header should be `Bearer <your_access_token>`, where `<your_access_token>` is the access token that you generated from the Google Cloud Platform console.
* `Body`: The text that you want to generate.
To configure the route, you will need to use the Kong Admin API. You can access the Kong Admin API by opening a web browser and navigating to https://localhost:8000/admin
Once you have opened the Kong Admin API, you can configure the route by clicking on the "Routes" tab and then clicking on the name of the route that you want to configure.
In the "Route Details" dialog, you will need to find the "Authorization" and "Body" fields. In the "Authorization" field, you should enter the value of the Authorization header that you generated from the Google Cloud Platform console. In the "Body" field, you should enter the text that you want to generate.
Once you have entered this information, you can click on the "Save" button to save the changes.
4. Test the route by sending a request to the Kong Gateway.
Once you have configured the route, you can test it by sending a request to the Kong Gateway. To do this, you can use the following curl command:
curl -X POST \
?-H "Authorization: Bearer <your_access_token>" \
?-H "Content-Type: application/json" \
?-d '{
??"text": "What is the meaning of life?"
?}' \
?https://localhost:8000/bard
If the request is successful, you will receive a response that contains the generated text.
Here is an example of a successful response:
{
?"text": "The meaning of life is to find your gift. The purpose of life is to give it away."
}
This is just an easy example. Use code snippets with caution.
Happy 'Barding' with Kong!