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

fix: support llama-index >= 0.11.0 #115

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions literalai/instrumentation/llamaindex.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import uuid
from typing import TYPE_CHECKING, Any, Dict, List, Optional, TypedDict, Union, cast

from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union, cast
from typing_extensions import TypedDict
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Python 3.11 fails otherwise

from llama_index.core.base.llms.types import MessageRole
from llama_index.core.base.response.schema import Response, StreamingResponse
from llama_index.core.instrumentation import get_dispatcher
Expand Down Expand Up @@ -30,7 +30,7 @@
from llama_index.core.schema import NodeWithScore, QueryBundle, TextNode
from openai.types import CompletionUsage
from openai.types.chat import ChatCompletion
from pydantic import Field
from pydantic import PrivateAttr

from literalai.context import active_thread_var
from literalai.observability.generation import ChatGeneration, GenerationMessageRole
Expand Down Expand Up @@ -111,8 +111,8 @@ def create_generation(event: LLMChatStartEvent):
class LiteralEventHandler(BaseEventHandler):
"""This class handles events coming from LlamaIndex."""

_client: "LiteralClient" = Field(...)
_span_handler: "LiteralSpanHandler" = Field(...)
_client: "LiteralClient" = PrivateAttr(...)
_span_handler: "LiteralSpanHandler" = PrivateAttr(...)
runs: Dict[str, List[Step]] = {}
streaming_run_ids: List[str] = []

Expand Down
42 changes: 42 additions & 0 deletions tests/e2e/test_llamaindex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
import urllib.parse

import pytest

from literalai import LiteralClient

from dotenv import load_dotenv

load_dotenv()


@pytest.fixture
def non_mocked_hosts() -> list:
non_mocked_hosts = []

# Always skip mocking API
url = os.getenv("LITERAL_API_URL", None)
if url is not None:
parsed = urllib.parse.urlparse(url)
non_mocked_hosts.append(parsed.hostname)

return non_mocked_hosts


@pytest.mark.e2e
class TestLlamaIndex:
@pytest.fixture(
scope="class"
) # Feel free to move this fixture up for further testing
def client(self):
url = os.getenv("LITERAL_API_URL", None)
api_key = os.getenv("LITERAL_API_KEY", None)
assert url is not None and api_key is not None, "Missing environment variables"

client = LiteralClient(batch_size=5, url=url, api_key=api_key)
client.instrument_llamaindex()

return client

async def test_instrument_llamaindex(self, client: "LiteralClient"):
assert client is not None
Loading