Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

79 - Tracing #81

Merged
merged 25 commits into from
Aug 15, 2024
Merged

79 - Tracing #81

merged 25 commits into from
Aug 15, 2024

Conversation

buckyroberts
Copy link
Contributor

@buckyroberts buckyroberts commented Aug 15, 2024

Closes #79

Basic example

import BaseOpenAI from "openai";
import {PromptLayer} from "@/index";

const promptlayer = new PromptLayer({
  apiKey: process.env.PROMPTLAYER_API_KEY,
  enableTracing: true,
  workspaceId: 1,
});

const OpenAI: typeof BaseOpenAI = promptlayer.OpenAI;
const openai = new OpenAI();

openai.chat.completions.create({
  messages: [{role: "user", content: "Sup bro?"}],
  model: "gpt-3.5-turbo",
  // @ts-ignore
  pl_tags: ["test"],
});

Custom (non-PromptLayer/LLM) function tracing

import {PromptLayer} from "@/index";

const promptlayer = new PromptLayer({
  apiKey: process.env.PROMPTLAYER_API_KEY,
  enableTracing: true,
  workspaceId: 1,
});

async function myCustomFunction(param1: number, param2: number) {
  await new Promise(resolve => setTimeout(resolve, 1000));
  return param1 * param2;
}

const wrappedFunction = promptlayer.wrapWithSpan('myCustomFunction', myCustomFunction, {
  customAttribute1: 'value1',
  customAttribute2: 'value2'
});

async function main() {
  try {
    const param1 = 5;
    const param2 = 7;
    await wrappedFunction(param1, param2);
  } catch (error) {
    console.error("Error in main function:", error);
  }
}

main().then(() => console.log("Script execution completed."));

.run()

import {PromptLayer} from "@/index";

const promptlayer = new PromptLayer({
  apiKey: process.env.PROMPTLAYER_API_KEY,
  enableTracing: true,
  workspaceId: 1,
});

const runPromptExample = async () => {
  try {
    await promptlayer.run({
      promptName: "ai-poet",
      inputVariables: {
        topic: "beans"
      },
      tags: ["geography", "test"],
      metadata: {
        user_id: "12345",
        session_id: "abcde"
      },
      stream: false
    });
  } catch (error) {
    console.error("Error running prompt:", error);
  }
}

runPromptExample();

Anthropic streaming

import {PromptLayer} from "@/index";

const promptlayer = new PromptLayer({
  apiKey: process.env.PROMPTLAYER_API_KEY,
  enableTracing: true,
  workspaceId: 1,
});

const Anthropic = promptlayer.Anthropic;
const anthropic = new Anthropic();

async function streamResponse(prompt: any) {
  const messages = [
    { role: 'user', content: prompt }
  ];

  const stream = await anthropic.messages.create({
    model: 'claude-3-opus-20240229',
    max_tokens: 30,
    messages: messages,
    stream: true
  });

  let fullResponse = '';
  for await (const chunk of stream) {
    if (chunk.delta?.text) {
      process.stdout.write(chunk.delta.text);
      fullResponse += chunk.delta.text;
    }
  }

  console.log('\n\nFull response:', fullResponse);
}

async function main() {
  const prompt = "Explain the concept of beans.";
  await streamResponse(prompt);
}

main().catch(console.error);

OpenAI streaming

import {PromptLayer} from "@/index";

const promptlayer = new PromptLayer({
  apiKey: process.env.PROMPTLAYER_API_KEY,
  enableTracing: true,
  workspaceId: 1,
});

const OpenAI = promptlayer.OpenAI;
const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

async function streamResponse(prompt: any) {
  const stream = await openai.chat.completions.create({
    model: 'gpt-4',
    messages: [{ role: 'user', content: prompt }],
    stream: true,
    pl_tags: ["streaming-example"],
  });

  let fullResponse = '';
  for await (const chunk of stream) {
    const content = chunk.choices[0]?.delta?.content || '';
    if (content) {
      process.stdout.write(content);
      fullResponse += content;
    }
  }

  console.log('\n\nFull response:', fullResponse);

  return fullResponse;
}

async function main() {
  const prompt = "Explain the concept of quantum computing in simple terms.";
  await streamResponse(prompt);
}

main().catch(console.error);

@buckyroberts buckyroberts marked this pull request as ready for review August 15, 2024 14:51
@buckyroberts buckyroberts requested a review from jzone3 as a code owner August 15, 2024 14:51
@buckyroberts buckyroberts merged commit 3c08172 into master Aug 15, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tracing
1 participant