Introduction to Foundry
Learn what Foundry is and how to use it in your LLM apps.
Foundry is a framework-agnostic toolset to create and use Functions in LLM apps.
When you create Functions with Foundry, they are organized in class
es called Tools. For example, the Calculator
Tool contains a calculate
Function that can be used to perform calculations by passing in a math string.
Tools and Functions can easily be passed to LLMs like OpenAI’s GPT-4 without writing custom JSON Schema definitions. The way they are defined allow for a generation of the JSON Schema on the fly by inferring the required arguments, names and descriptions.
Btw, we’d love to welcome you as a contributor to Foundry!
When to use Foundry?
- Whenever you want to integrate Functions into your LLM apps without the need to write custom JSON Schema definitions.
- You want to build LLM applications that have broad capabilities and can be used for a variety of tasks.
Why use Foundry?
- Community effort: We want to build a great community of developers that are passionate about building LLM apps. Tools are written by developers, for developers.
- Easy to use: Foundry is designed to be the easiest way to expose Functions to LLMs.
- Customizable: You can easily add custom Tools and Functions and use them in your LLM apps.
- Framework-agnostic: Foundry can be used in combination with any framework, e.g. LangChain, LlamaIndex or directly with OpenAI’s SDK.
- Standalone functions: Functions can be used without the need for any LLMs or the
Foundry
class. Just install the respective Tool and call the function directly.
Quickstart
Learn how to get started with Foundry in your codebase.
Tools
Browse the Tools that are available in Foundry.
Examples
Using GPT-4 as LLM
In this example, we use OpenAI’s GPT-4 model to evaluate a given prompt and run the appropriate function from the Foundry tools.
Note how the getPreparedFunctions
dynamically generates the Function definitions in the way OpenAI expects them.
After the LLM has predicted the Function to run, we can use runSelectedFunction
to run the Function with the computed arguments to get the desired result.
import { Foundry } from '@usefoundry/foundry'
import { Configuration, OpenAIApi } from 'openai'
import WeatherApiTool from '@usefoundry/tools-api-weather-api'
import CsvTool from '@usefoundry/tools-file-csv'
import CalculatorTool from '@usefoundry/tools-utils-calculator'
// Create a foundry instance with the tools we want to use
const foundry = new Foundry({
tools: [
new WeatherApiTool({
apiKey: process.env.WEATHER_API_KEY!,
}),
new CsvTool(),
new CalculatorTool(),
],
})
// Initialize OpenAI API
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY!,
})
const openai = new OpenAIApi(configuration)
const runPrompt = async ({ userPrompt }: { userPrompt: string }) => {
// Set some context for the prompt
const prompt = `
Context:
- Current date: ${new Date().toDateString()}
Prompt:
${userPrompt}
`
//! Get all functions from the Foundry tools defined above
// They are formatted in the way OpenAI expects them
const functions = foundry.getPreparedFunctions({ target: 'openai' })
// Run the prompt against the OpenAI model
const response = await openai.createChatCompletion({
functions: functions,
function_call: 'auto',
messages: [
{
role: 'user',
content: prompt,
},
],
model: 'gpt-4-0613',
temperature: 0,
})
const targetFunctionProps = response.data.choices[0].message?.function_call
if (!targetFunctionProps) {
throw new Error('No function found in response')
}
const functionResult = foundry.runSelectedFunction(targetFunctionProps)
return functionResult
}
await runPrompt({ userPrompt: 'What is the weather in Berlin today?' })
await runPrompt({ userPrompt: 'What is 2 + 2?' })
await runPrompt({
userPrompt: 'Write a list of the latest 10 US presidents into ./presidents.csv',
})
await runPrompt({ userPrompt: 'Get a random number between 30 and 60' })
Standalone
You can also use the functions from Foundry standalone, without LLMs or the Foundry
class.
import CsvTool from '@usefoundry/tools-file-csv'
const csv = new CsvTool()
const presidents = [
{
name: 'Joe Biden',
party: 'Democratic',
},
{
name: 'Donald Trump',
party: 'Republican',
},
{
name: 'Barack Obama',
party: 'Democratic',
},
]
await csv.writeCsvFileSync({
data: presidents,
path: './presidents.csv',
})