Chainlit / literalai-python

https://docs.getliteral.ai/
Apache License 2.0
12 stars 0 forks source link

feat(instrumentation): update Llama index instrumentation #92

Closed Dam-Buty closed 3 months ago

Dam-Buty commented 3 months ago

This updates the Llamaindex integration to their new Event/Span driven architecture.

We get a stream of Spans, which are nested into each other. Each Event is associated to a Span. Here is the conceptual mapping :

The following code :

from literalai import LiteralClient
from llama_index.core import Document, VectorStoreIndex

client = LiteralClient()
client.instrument_llamaindex()

print("Vectorizing documents")
index = VectorStoreIndex.from_documents([Document.example()])

print()
print("Sending query")
print()

query_engine = index.as_query_engine()
query_engine.query("Tell me about LLMs?")
query_engine = index.as_query_engine(streaming=True)
query_engine.query("Stream : Tell me about LLMs?")

client.flush_and_stop()

will result in the following Literal AI tree :

image

linear[bot] commented 3 months ago
ENG-1616 update llama index instrumentation

[https://docs.llamaindex.ai/en/stable/module_guides/observability/instrumentation/](https://docs.llamaindex.ai/en/stable/module_guides/observability/instrumentation/) Arize did this [https://www.linkedin.com/posts/llamaindex_were-excited-to-feature-llamatrace-a-collaborative-activity-7217228600381440002-fr87/?utm_source=share&utm_medium=member_android](https://www.linkedin.com/posts/llamaindex_were-excited-to-feature-llamatrace-a-collaborative-activity-7217228600381440002-fr87/?utm_source=share&utm_medium=member_android) They also pushed the integration directly to llama index apparently. We should do to and make a joint post. We should have the instrumentation in our SDK and one API key away in Llama. We already have a ticket for this, I added this as a related issue.

desaxce commented 3 months ago

If the query_engine creates a thread -> its name should default to the first user message

I'm getting an error now. I'm checking what's wrong.

>>> query_engine.query("Tell me about M&Ms?")
ERROR:root:[Literal] Error in Llamaindex instrumentation : 'ChatCompletion' object is not subscriptable
Traceback (most recent call last):
  File "/Users/desaxce/Documents/python-client/literalai/instrumentation/llamaindex.py", line 230, in handle
    usage = response["raw"]["usage"]
            ~~~~~~~~~~~~~~~^^^^^^^^^
TypeError: 'ChatCompletion' object is not subscriptable
Response(response="I'm sorry, but based on the context provided, there is no information about M&Ms.", source_nodes=[NodeWithScore(node=TextNode(id_='b1594afd-a384-47ee-a074-144dbce98c90', embedding=None, metadata={'filename': 'README.md', 'category': 'codebase'}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={<NodeRelationship.SOURCE: '1'>: RelatedNodeInfo(node_id='87527f78-314b-467e-b2a6-820918d12408', node_type=<ObjectType.DOCUMENT: '4'>, metadata={'filename': 'README.md', 'category': 'codebase'}, hash='3183371414f6a23e9a61e11b45ec45f808b148f9973166cfed62226e3505eb05')}, text='Context
LLMs are a phenomenal piece of technology for knowledge generation and reasoning.
They are pre-trained on large amounts of publicly available data.
How do we best augment LLMs with our own private data?
We need a comprehensive toolkit to help perform this data augmentation for LLMs.

Proposed Solution
That\'s where LlamaIndex comes in. LlamaIndex is a "data framework" to help
you build LLM  apps. It provides the following tools:

Offers data connectors to ingest your existing data sources and data formats
(APIs, PDFs, docs, SQL, etc.)
Provides ways to structure your data (indices, graphs) so that this data can be
easily used with LLMs.
Provides an advanced retrieval/query interface over your data:
Feed in any LLM input prompt, get back retrieved context and knowledge-augmented output.
Allows easy integrations with your outer application framework
(e.g. with LangChain, Flask, Docker, ChatGPT, anything else).
LlamaIndex provides tools for both beginner users and advanced users.
Our high-level API allows beginner users to use LlamaIndex to ingest and
query their data in 5 lines of code. Our lower-level APIs allow advanced users to
customize and extend any module (data connectors, indices, retrievers, query engines,
reranking modules), to fit their needs.', mimetype='text/plain', start_char_idx=1, end_char_idx=1279, text_template='{metadata_str}

{content}', metadata_template='{key}: {value}', metadata_seperator='
'), score=0.7154313189988025)], metadata={'b1594afd-a384-47ee-a074-144dbce98c90': {'filename': 'README.md', 'category': 'codebase'}})
>>> 
desaxce commented 3 months ago

On a couple of runs I had this output: image

I couldn't reproduce though. Maybe it's a LLamaIndex bug.