Skip to content

Commit

Permalink
fix(ai): readme (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
k11kirky authored Jan 22, 2025
1 parent 89b0c68 commit b649be7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
51 changes: 19 additions & 32 deletions posthog-ai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,43 @@ Initial Typescript SDK for LLM Observability
## Installation

```bash
yarn add @posthog/posthog-ai
npm install @posthog/ai
```

## Usage

### Before

```typescript
import { OpenAI } from 'openai'

const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY || '',
})

await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello, world!' }],
})
```

### After

```typescript
import { OpenAI } from '@posthog/ai'
import { PostHog } from 'posthog-node'

const phClient = new PostHog(
process.env.POSTHOG_API_KEY, {
host: process.env.POSTHOG_HOST || 'https://us.posthog.com',
}
})
'<YOUR_PROJECT_API_KEY>',
{ host: 'https://us.i.posthog.com' }
);

const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY || '',
apiKey: '<YOUR_OPENAI_API_KEY>',
posthog: phClient,
})
});

await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello, world!' }],
posthogDistinctId: 'test-user-id',
posthogProperties: {
testProperty: 'testValue',
}
})
const completion = await client.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: "Tell me a fun fact about hedgehogs" }],
posthogDistinctId: "user_123", // optional
posthogTraceId: "trace_123", // optional
posthogProperties: { conversation_id: "abc123", paid: true }, //optional
posthogGroups: { company: "company_id_in_your_db" }, // optional
posthogPrivacyMode: false // optional
});

console.log(completion.choices[0].message.content)

// YOU HAVE TO HAVE THIS OR THE CLIENT MAY NOT SEND EVENTS
await phClient.shutdown()
```

LLM Observability [docs](https://posthog.com/docs/ai-engineering/observability)

Please see the main [PostHog docs](https://www.posthog.com/docs).

## Questions?
Expand Down
2 changes: 1 addition & 1 deletion posthog-ai/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@posthog/ai",
"version": "2.0.0",
"version": "2.0.1",
"description": "PostHog Node.js AI integrations",
"repository": {
"type": "git",
Expand Down

0 comments on commit b649be7

Please sign in to comment.