Skip to content

Commit

Permalink
langbase example added (#912)
Browse files Browse the repository at this point in the history
<!-- ELLIPSIS_HIDDEN -->


> [!IMPORTANT]
> Adds a new example for generating a PRD using Langbase and
OpenAIToolSet with necessary dependencies.
> 
>   - **New Example**:
> - Adds `demo.js` to `js/examples/prd_generator/` for generating a PRD
using Langbase and OpenAIToolSet.
> - Utilizes `Langbase` to process user messages and optionally create a
Google Doc via `OpenAIToolSet`.
>   - **Dependencies**:
> - Adds `package.json` with dependencies: `composio-core`, `express`,
`langbase`, and `pusher-js`.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=ComposioHQ%2Fcomposio&utm_source=github&utm_medium=referral)<sup>
for c355c76. It will automatically
update as commits are pushed.</sup>


<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
Prat011 authored Nov 28, 2024
1 parent 86a7f5c commit bd6cc59
Show file tree
Hide file tree
Showing 3 changed files with 1,774 additions and 0 deletions.
111 changes: 111 additions & 0 deletions js/examples/prd_generator/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import 'dotenv/config';
import { Langbase } from 'langbase';
import {OpenAIToolSet} from 'composio-core';

// Initialize Langbase with API key from environment variables
const langbase = new Langbase({
apiKey: process.env.LANGBASE_API_KEY,
});

const GOOGLEDOCS_CREATE_DOCUMENT = async ({ title, text }) => {
const toolset = new OpenAIToolSet({
apiKey: process.env.COMPOSIO_API_KEY,
entityId: "default_user",
});
const response = await toolset.client.getEntity("default_user").execute(
'GOOGLEDOCS_CREATE_DOCUMENT',
{
title,
text
}
);
return response;
};

// Register the tools
const tools = {
GOOGLEDOCS_CREATE_DOCUMENT,
};

(async () => {
const messages = [
{
role: 'user',
content: 'Write a prd for a docker like platform on google docs'
}
];

// replace this with your Pipe API key
const pipeApiKey = ``;

const res = await fetch('https://api.langbase.com/v1/pipes/run', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.LANGBASE_API_KEY}`
},
body: JSON.stringify({
messages
})
});
const data = await res.json();
console.log('Response:', data);
// get the threadId from the response headers
const threadId = await res.headers.get('lb-thread-id');

const { raw } = data;

// get the response message from the model
const responseMessage = raw.choices[0].message;

// get the tool calls from the response message
const toolCalls = responseMessage.tool_calls;

if (toolCalls) {
const toolMessages = [];

// call all the functions in the tool_calls array
toolCalls.forEach(toolCall => {
const toolName = toolCall.function.name;
console.log('Tool Call:', toolCall);
console.log('Raw arguments:', toolCall.function.arguments);
try {
const toolParameters = JSON.parse(toolCall.function.arguments);
const toolFunction = tools[toolName];
const toolResponse = toolFunction(toolParameters);

toolMessages.push({
tool_call_id: toolCall.id,
role: 'tool',
name: toolName,
content: JSON.stringify(toolResponse),
});
} catch (error) {
console.error('Error parsing JSON arguments:', error);
console.error('Position:', error.message.match(/position (\d+)/)?.[1]);
if (error.message.includes('position')) {
const pos = parseInt(error.message.match(/position (\d+)/)[1]);
console.error('Problematic section:',
JSON.stringify(toolCall.function.arguments.slice(Math.max(0, pos - 50), pos + 50))
);
}
}
});

// send the tool responses back to the API
const res = await fetch('https://api.langbase.com/v1/pipes/run', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.LANGBASE_API_KEY}`,
},
body: JSON.stringify({
messages: toolMessages,
threadId,
}),
});

const data = await res.json();
console.log('Tool responses processed successfully:', data);
}
})();
Loading

0 comments on commit bd6cc59

Please sign in to comment.