This content originally appeared on Bits and Pieces - Medium and was authored by Ashan Fernando
Building GPT Actions with AWS Lambda using Bit Components
Creating GPT actions is an exciting way to connect your applications to the ChatGPT prompts. This is done by connecting your API endpoint and describing the API in OpenAPI specification. Internally, GPT Actions leverage Function Calling to execute API calls. Once defined, ChatGPT understands when to call based on the conversation you are having with it.
In this article, we explore how to build a GPT action using composable AWS Lambda functions with Bit components. This example focuses on creating a weather API action where ChatGPT interacts with real-time weather data and responds to a weather-related prompt.
Action Overview
ChatGPT action that uses the weather API processes the user prompts that map into the weather Action inputs (also transforms any inputs if required) to directly retrieve real-time weather data. Here’s how it works:
- User Prompt and Intent: ChatGPT identifies the intent to check the weather and uses an OpenAPI schema to understand how to make the request.
- Schema-Guided Request: The schema tells ChatGPT which endpoint to use, the required parameters, and the response format, ensuring a valid API call.
- Lambda Execution: The request is sent to an AWS Lambda function hosting the weather API, which fetches live weather data for the specified location.
- Response to User: ChatGPT interprets the API response and returns the weather details conversationally.
The OpenAPI schema enables ChatGPT to automate requests and responses, creating a seamless, real-time interaction for users.
API with Components
Creating the API using AWS Lambda and components offers several advantages. AWS Lambda allows you to create the API with a pay-as-you-go model and a generous free tier. At the same time, components increase maintainability and support continuous updates and backward compatibility.
To begin with, we design a composable API that will serve as the foundation for our GPT action. This API will fetch weather data from the Open Weather Service. Let's create each component separately.
- GPT Action Entity: Defines the API structure and parameters.
- GPT Action Logic: Connects the API with ChatGPT to interpret and respond to weather requests.
- GPT Action (Lambda): This action handles requests and exposes two endpoints, one for weather data and another for returning the OpenAPI Schema.
Each component will be developed as an independent Bit component, allowing isolated testing and smooth integration within the larger GPT action architecture.
Step 1: Creating the GPT Action Entity
This component handles weather data using the Weather interface, which defines the structure of weather information fetched from external APIs (like OpenWeatherMap).
You can find the component here. Creating this was easy by following the below steps.
- Create an account in bit.cloud.
- Create a scope (e.g my-gpt-scope) to store your components.
- Go to the scope URL and click the “Edit Scope” button in the top right corner. This immediately spins up a Bit Cloud Workspace instance with ready-made tooling to create new components.
4. Create a new component. (You can find all the component templates available by running bit templates command).
bit create entity "gpt-action-entity"
5. Update your component code to return the weather schema and update its unit tests (spec file).
export type Weather = {
main: {
temp: number; // Temperature in Celsius
};
weather: {
description: string; // Weather condition description
}[];
name: string; // City name
};
Step 1: Creating the GPT Action Logic
Similar to above, create a new entity with logic to fetch weather data from Open Weather API. To create the component lets use the module template.
bit create module "gpt-action-logic"
import type { Weather } from "@aws-lambda/demos.gpt-action-entity";
import axios from 'axios';
/**
* Fetches weather data based on the provided city and apiKey.
*
* @param city The city for which to fetch weather data.
* @param apiKey The API key for accessing the weather data.
* @returns A string with the weather data or an error message.
*/
export async function gptActionLogic(city: string, apiKey: string): Promise<string> {
// Move the getWeather logic inside gptActionLogic
const getWeather = async (city: string, apiKey: string): Promise<Weather> => {
const apiUrl = `http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}&units=metric`;
const response = await axios.get(apiUrl);
return response.data as Weather; // Cast response data to the Weather type
};
try {
const weather = await getWeather(city, apiKey);
return `It's ${weather.main.temp}°C in ${city} with ${weather.weather[0].description}.`;
} catch (error) {
return 'Failed to fetch weather data. Please try again.';
}
}
The entity components are imported and used in its implementation to bind weather data. You can find the component here.
Step 3: Create the GPT Action (Function)
In this step, we will create the AWS Lambda function component that acts as the API to return the weather data and its Open API schema.
For this, instead of creating a component from scratch, let's clone an existing Lambda component and modify its code to retrieve weather data from the GPT Action Logic component.
bit fork aws-lambda.demos/http-lambda@0.0.5 gpt-action
After copying it, you need to modify its environment variables (to pass the API key for Open Weather API) and expose two endpoints.
- Schema Endpoint: This exposes a predefined API schema in the OpenAPI 3.1 specification. By providing your function implementation, you can easily generate this using HopeAI (built-in AI with Bit) or ChatGPT.
- Weather Endpoint: This responds to the API request to retrieve weather information.
You can find the implementation of the function here. Once all the components are connected, the dependency graph looks like the following.
After defining the function, you can also set the WEATHER API KEY, AWS ACCESS KEY IDand AWS SECRET ACCESS KEYin your scopes, Ripple CI variables and tag and export the components into Bit cloud. This triggers the Ripple CI to deploy your functions into AWS and shows the HTTP URL you can use to invoke the API.
Connecting the Dots
Finally, you can register this endpoint in ChatGPT's MyGPT section. You need a paid ChatGPT plan for this.
To do this, follow the below steps.
Open Top Right Menu -> My GPTs -> Create a GPT -> Fill in the Information -> Create new Action
You can import the schema using the “import from URL” button and test your action. Ask a question like, “What’s the weather in New York?” Then ChatGPT knows it needs to invoke the API instead of generating the results from the LLM.
Conclusion
By building a GPT action with AWS Lambda functions and Bit components, we’ve created a simple API that allows ChatGPT to interact with real-time weather data based on user prompts.
This component-based approach allows scaling the implementation into multiple functions, sharing logic components in between as and when required. Overall, this structure supports a reliable, scalable way to extend ChatGPT’s functionality with third-party integrations.
I hope you found this article useful. Thanks for Reading. Cheers!!
Learn More
- Hope AI: A Code Assistant for Composable Software
- AWS Lambda Development at Scale: Using Composable Architecture
- Cell Architecture and Micro Frontends: Building Composable Applications
- Composable Software Architectures are Trending: Here’s Why
New GPT Actions Powered by Components was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Bits and Pieces - Medium and was authored by Ashan Fernando
Ashan Fernando | Sciencx (2024-10-28T15:25:31+00:00) New GPT Actions Powered by Components. Retrieved from https://www.scien.cx/2024/10/28/new-gpt-actions-powered-by-components/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.