Installation
We provide a fully typed Javascript SDK that allows you to create apps with your own code, and use Llama Workspace as the UI. Check the example below to see how it works.
Package installation
The SDK is available as an npm package. To install it simply run:
npm install llamaworkspace
Example usage
The following example demonstrates how to use quickly start using the SDK to interact with Llama Workspace.
import { LlamaWorkspace } from "llamaworkspace";
// Get the accessKey by creating an "External app" in Llama Workspace
const ACCESS_KEY = "<access-key>";
const llamaWorkspace = new LlamaWorkspace({
accessKey: ACCESS_KEY,
});
export async function handleIncomingRequest(body) {
// Validate the incoming body and get a typed object
const incomingBody = llamaWorkspace.validatePayloadOrThrow(body);
const { accessKey, data } = incomingBody;
// Verify that the accessKey is valid
llamaWorkspace.verifyAccessKeyOrThrow(accessKey);
// Wrap your custom logic inside a function
// that provides you access to utils to send data back to Llama Workspace
const stream = llamaWorkspace.asStream(async ({ pushText }) => {
const { messages } = data;
// Implement your AI logic here, and push the streaming results
// back to the to the client; chunk by chunk.
const myAIStreamedResponse = await myAIAgentProcess(messages);
for await (const chunk of myAIStreamedResponse) {
// pushText sends the chunk back to Llama Workspace
pushText(chunk);
}
});
// Return the stream back to Llama Workspace
return new Response(stream, {
headers: {
"Content-Type": "text/event-stream",
},
});
}