Querying NASA Image and Video Library with LangChain NasaAPIWrapper

Posted: Nov 12, 2024.

The NASA Image and Video Library provides a vast collection of space-related media content. LangChain's NasaAPIWrapper allows you to programmatically access this library and build applications that can search and retrieve NASA's media assets.

What is NasaAPIWrapper?

NasaAPIWrapper is a utility class that provides a Python interface to NASA's Image and Video Library API. It allows you to:

  • Search for media (images and videos)
  • Retrieve metadata about media assets
  • Get video caption information
  • Access media manifest data

Reference

Here's a breakdown of the available methods in NasaAPIWrapper:

MethodDescription
get_media(query: str)Search and retrieve media assets matching the query string
get_media_metadata_location(query: str)Get the location metadata for a specific media asset
get_media_metadata_manifest(query: str)Get the manifest metadata containing detailed information about a media asset
get_video_captions_location(query: str)Get the location of captions for a video asset
run(mode: str, query: str)Generic method to run any of the above operations by specifying the mode

How to Use NasaAPIWrapper

Basic Setup

First, you'll need to import and initialize the wrapper:

from langchain_community.utilities.nasa import NasaAPIWrapper

nasa = NasaAPIWrapper()

Searching for Media

To search for media assets in NASA's library:

# Search for moon images
result = nasa.get_media("moon landing apollo 11")
print(result)

Getting Media Metadata

You can retrieve metadata about specific media assets using their NASA ID:

# Get metadata location for a specific media asset
metadata_location = nasa.get_media_metadata_location("NHQ_2019_0311_Go Forward to the Moon")

# Get detailed manifest metadata
manifest = nasa.get_media_metadata_manifest("NHQ_2019_0311_Go Forward to the Moon")

Using with LangChain Agents

NasaAPIWrapper can be integrated with LangChain's agent system for more sophisticated applications:

from langchain.agents import AgentType, initialize_agent
from langchain_community.agent_toolkits.nasa.toolkit import NasaToolkit
from langchain_openai import OpenAI

# Initialize components
llm = OpenAI(temperature=0)
nasa = NasaAPIWrapper()
toolkit = NasaToolkit.from_nasa_api_wrapper(nasa)

# Create an agent
agent = initialize_agent(
    toolkit.get_tools(), 
    llm, 
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, 
    verbose=True
)

# Use the agent to search for media
response = agent.run("Find three pictures of the moon published between 2014 and 2020")

Working with Video Captions

If you're working with video content, you can retrieve caption information:

# Get caption location for a video
captions_location = nasa.get_video_captions_location("video_nasa_id")

Important Notes

  • When using the search functionality, be aware that queries can return large amounts of data if not properly constrained
  • You should handle the responses appropriately as they contain JSON data that needs to be parsed
  • The wrapper is particularly useful when combined with LangChain's agent system for natural language interactions with NASA's media library
  • No API key is required to use NASA's Image and Video Library API, making it easily accessible for development

The NasaAPIWrapper provides a convenient way to access NASA's vast media library programmatically. Whether you're building an educational tool, a space-focused application, or just want to explore NASA's media assets, this wrapper simplifies the process of interacting with NASA's API.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs

Join 10,000+ subscribers

Every 2 weeks, latest model releases and industry news.

An alternative to LangSmith

Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.

LangChain Docs