mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
Mechanically re-derived: bin/debrand runs over a fresh import of posthog/posthog@fa1a9a60d4 (2026-08-06). The pipeline (.hanzo/workflows), the Dockerfiles and bin/docker* entrypoints, compose files, LLM.md and the squashed initial migration are restored from the shipped tree, same as the previous import. The prune's import-closure guard kept 21 products the new core now imports; dropping them is a refactor now, recorded rather than forced.
142 lines
6.4 KiB
TypeScript
142 lines
6.4 KiB
TypeScript
import { OnboardingComponentsContext, createInstallation } from 'scenes/onboarding/shared/OnboardingDocsContentWrapper'
|
|
|
|
import { StepDefinition } from '../steps'
|
|
|
|
export const getLlamaIndexSteps = (ctx: OnboardingComponentsContext): StepDefinition[] => {
|
|
const { CodeBlock, CalloutBox, Markdown, Blockquote, dedent, snippets } = ctx
|
|
|
|
const NotableGenerationProperties = snippets?.NotableGenerationProperties
|
|
|
|
return [
|
|
{
|
|
title: 'Install dependencies',
|
|
badge: 'required',
|
|
content: (
|
|
<>
|
|
<CalloutBox type="info" icon="IconInfo" title="Full working examples">
|
|
<Markdown>
|
|
See the complete [Python
|
|
example](https://github.com/Insights/insights-python/tree/master/examples/example-ai-llamaindex)
|
|
on GitHub. If you use the Insights SDK wrapper instead of OpenTelemetry, see this example
|
|
instead: [Python wrapper
|
|
example](https://github.com/Insights/insights-python/tree/7223c52/examples/example-ai-llamaindex).
|
|
</Markdown>
|
|
</CalloutBox>
|
|
|
|
<Markdown>
|
|
Install LlamaIndex, OpenAI, and the OpenTelemetry SDK with the LlamaIndex instrumentation.
|
|
</Markdown>
|
|
|
|
<CodeBlock
|
|
language="bash"
|
|
code={dedent`
|
|
pip install llama-index llama-index-llms-openai opentelemetry-sdk "insights[otel]" opentelemetry-instrumentation-openai-v2
|
|
`}
|
|
/>
|
|
</>
|
|
),
|
|
},
|
|
{
|
|
title: 'Set up OpenTelemetry tracing',
|
|
badge: 'required',
|
|
content: (
|
|
<>
|
|
<Markdown>
|
|
Configure OpenTelemetry to auto-instrument LlamaIndex calls and export traces to Insights.
|
|
Insights converts `gen_ai.*` spans into `$ai_generation` events automatically.
|
|
</Markdown>
|
|
|
|
<CodeBlock
|
|
language="python"
|
|
code={dedent`
|
|
from opentelemetry import trace
|
|
from opentelemetry.sdk.trace import TracerProvider
|
|
from opentelemetry.sdk.resources import Resource, SERVICE_NAME
|
|
from insights.ai.otel import InsightsSpanProcessor
|
|
from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor
|
|
|
|
resource = Resource(attributes={
|
|
SERVICE_NAME: "my-app",
|
|
"insights.distinct_id": "user_123", # optional: identifies the user in Insights
|
|
"foo": "bar", # custom properties are passed through
|
|
})
|
|
|
|
provider = TracerProvider(resource=resource)
|
|
provider.add_span_processor(
|
|
InsightsSpanProcessor(
|
|
api_key="<ph_project_token>",
|
|
host="<ph_client_api_host>",
|
|
)
|
|
)
|
|
trace.set_tracer_provider(provider)
|
|
|
|
OpenAIInstrumentor().instrument()
|
|
`}
|
|
/>
|
|
|
|
<CalloutBox type="fyi" icon="IconInfo" title="What gets captured">
|
|
<Markdown>
|
|
This instruments the OpenAI calls LlamaIndex makes underneath, so you get one
|
|
`$ai_generation` per LLM call. Retrieval and query-engine steps are not captured as spans.
|
|
To record those, capture `$ai_span` events yourself with a shared `$ai_trace_id`. See
|
|
[manual capture](https://hanzo.ai/docs/ai-observability/installation/manual-capture).
|
|
</Markdown>
|
|
</CalloutBox>
|
|
</>
|
|
),
|
|
},
|
|
{
|
|
title: 'Query with LlamaIndex',
|
|
badge: 'required',
|
|
content: (
|
|
<>
|
|
<Markdown>
|
|
Use LlamaIndex as normal. The OpenTelemetry instrumentation automatically captures
|
|
`$ai_generation` events for each LLM call.
|
|
</Markdown>
|
|
|
|
<CodeBlock
|
|
language="python"
|
|
code={dedent`
|
|
from llama_index.llms.openai import OpenAI
|
|
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
|
|
|
llm = OpenAI(model="gpt-4o-mini", api_key="your_openai_api_key")
|
|
|
|
# Load your documents
|
|
documents = SimpleDirectoryReader("data").load_data()
|
|
|
|
# Create an index
|
|
index = VectorStoreIndex.from_documents(documents, llm=llm)
|
|
|
|
# Query the index
|
|
query_engine = index.as_query_engine(llm=llm)
|
|
response = query_engine.query("What is this document about?")
|
|
|
|
print(response)
|
|
`}
|
|
/>
|
|
|
|
<Blockquote>
|
|
<Markdown>
|
|
**Note:** If you want to capture LLM events anonymously, omit the `insights.distinct_id`
|
|
resource attribute. See our docs on [anonymous vs identified
|
|
events](https://hanzo.ai/docs/data/anonymous-vs-identified-events) to learn more.
|
|
</Markdown>
|
|
</Blockquote>
|
|
|
|
<Markdown>
|
|
{dedent`
|
|
You can expect captured \`$ai_generation\` events to have the following properties:
|
|
`}
|
|
</Markdown>
|
|
|
|
{NotableGenerationProperties && <NotableGenerationProperties />}
|
|
</>
|
|
),
|
|
},
|
|
]
|
|
}
|
|
|
|
export const LlamaIndexInstallation = createInstallation(getLlamaIndexSteps)
|