Skip to content

Add autodoc script #75

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

Closed
wants to merge 10 commits into from
Closed
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
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified examples/.env.example
100644 → 100755
Empty file.
Empty file modified examples/attachment.py
100644 → 100755
Empty file.
Empty file modified examples/dataset.py
100644 → 100755
Empty file.
Empty file modified examples/file-upload.py
100644 → 100755
Empty file.
Empty file modified examples/langchain_example.py
100644 → 100755
Empty file.
Empty file modified examples/main.py
100644 → 100755
Empty file.
Empty file modified examples/parallel_requests.py
100644 → 100755
Empty file.
Empty file modified examples/prompt.py
100644 → 100755
Empty file.
Empty file modified examples/samplefile.txt
100644 → 100755
Empty file.
Empty file modified examples/streaming.py
100644 → 100755
Empty file.
Empty file modified examples/thread.py
100644 → 100755
Empty file.
Empty file modified examples/user.py
100644 → 100755
Empty file.
Empty file modified literalai/__init__.py
100644 → 100755
Empty file.
14 changes: 12 additions & 2 deletions literalai/api/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,16 @@ def list_threads(
"""
Lists threads based on pagination and optional filters, similar to get_threads but may include additional processing.

```
from literalai.thread import DateTimeFilter, ThreadFilter

filters = ThreadFilter(
createdAt=DateTimeFilter(operator="gt", value="2023-12-14"),
)

page = 1
result = literal_client.api.list_threads(filters=filters)
```
Args:
first (Optional[int]): Number of threads to list.
after (Optional[str]): Cursor for pagination, list threads after this cursor.
Expand Down Expand Up @@ -949,7 +959,7 @@ def send_steps(self, steps: List[Union[StepDict, "Step"]]):
Sends a list of steps to be processed.

Args:
steps (List[Union[StepDict, "Step"]]): A list of steps or step dictionaries to send.
steps (List[Union[StepDict, Step]]): A list of steps or step dictionaries to send.

Returns:
The result of the GraphQL helper function for sending steps.
Expand Down Expand Up @@ -2118,7 +2128,7 @@ async def send_steps(self, steps: List[Union[StepDict, "Step"]]):
Asynchronously sends a list of steps to be processed.

Args:
steps (List[Union[StepDict, "Step"]]): A list of steps or step dictionaries to send.
steps (List[Union[StepDict, Step]]): A list of steps or step dictionaries to send.

Returns:
The result of the GraphQL helper function for sending steps.
Expand Down
Empty file modified literalai/api/attachment_helpers.py
100644 → 100755
Empty file.
Empty file modified literalai/api/dataset_helpers.py
100644 → 100755
Empty file.
Empty file modified literalai/api/generation_helpers.py
100644 → 100755
Empty file.
Empty file modified literalai/api/gql.py
100644 → 100755
Empty file.
Empty file modified literalai/api/prompt_helpers.py
100644 → 100755
Empty file.
Empty file modified literalai/api/score_helpers.py
100644 → 100755
Empty file.
Empty file modified literalai/api/step_helpers.py
100644 → 100755
Empty file.
Empty file modified literalai/api/thread_helpers.py
100644 → 100755
Empty file.
Empty file modified literalai/api/user_helpers.py
100644 → 100755
Empty file.
Empty file modified literalai/callback/__init__.py
100644 → 100755
Empty file.
Empty file modified literalai/callback/langchain_callback.py
100644 → 100755
Empty file.
Empty file modified literalai/callback/llama_index_callback.py
100644 → 100755
Empty file.
171 changes: 170 additions & 1 deletion literalai/client.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@


class BaseLiteralClient:
"""
<Warning>Use LiteralClient or AsyncLiteralClient in your code. Both classes inherit from BaseLiteralClient and have access to its methods.</Warning>

Attributes:
api (Union[LiteralAPI, AsyncLiteralAPI]): Instance of the Literal API.
disabled (bool): Flag indicating if the client is disabled.
event_processor (EventProcessor): Processor for handling events.

Args:
batch_size (int, optional): Batch size for event processing. Defaults to 5.
is_async (bool, optional): Flag indicating if the API should be asynchronous. Defaults to False.
api_key (Optional[str], optional): API key for authentication. If not provided, it will be fetched from the environment variable 'LITERAL_API_KEY'.
url (Optional[str], optional): URL of the Literal API. Defaults to 'https://cloud.getliteral.ai'.
disabled (bool, optional): Flag to disable the client. Defaults to False.

Raises:
Exception: If 'LITERAL_API_KEY' is not provided and not found in environment variables.
"""

api: Union[LiteralAPI, AsyncLiteralAPI]

def __init__(
Expand Down Expand Up @@ -50,6 +69,12 @@ def __init__(
)

def to_sync(self) -> "LiteralClient":
"""
Convert the client to synchronous mode.

Returns:
LiteralClient: Synchronous LiteralClient instance.
"""
if isinstance(self.api, AsyncLiteralAPI):
return LiteralClient(
batch_size=self.event_processor.batch_size,
Expand All @@ -61,6 +86,9 @@ def to_sync(self) -> "LiteralClient":
return self # type: ignore

def instrument_openai(self):
"""
Instruments the OpenAI API calls with the current client.
"""
instrument_openai(self.to_sync())

def langchain_callback(
Expand All @@ -69,6 +97,17 @@ def langchain_callback(
to_keep: Optional[List[str]] = None,
**kwargs: Any,
):
"""
Get a Langchain callback tracer configured with the client.

Args:
to_ignore (Optional[List[str]], optional): List of event types to ignore. Defaults to None.
to_keep (Optional[List[str]], optional): List of event types to keep. Defaults to None.
**kwargs: Additional arguments for the tracer.

Returns:
LangchainTracer: Configured Langchain tracer.
"""
LangchainTracer = get_langchain_callback()
return LangchainTracer(
self.to_sync(),
Expand All @@ -81,6 +120,15 @@ def llama_index_callback(
self,
**kwargs: Any,
):
"""
Get a Llama Index callback tracer configured with the client.

Args:
**kwargs: Additional arguments for the tracer.

Returns:
LlamaIndexTracer: Configured Llama Index tracer.
"""
LlamaIndexTracer = get_llama_index_callback()
return LlamaIndexTracer(
self.to_sync(),
Expand All @@ -95,6 +143,18 @@ def thread(
name: Optional[str] = None,
**kwargs,
):
"""
Decorator or context manager for thread-level event grouping.

Args:
original_function (callable, optional): Function to decorate. Defaults to None.
thread_id (Optional[str], optional): ID of the thread. Defaults to None.
name (Optional[str], optional): Name of the thread. Defaults to None.
**kwargs: Additional arguments for the thread context manager or decorator.

Returns:
Union[ThreadContextManager, callable]: Thread context manager or decorated function.
"""
if original_function:
return thread_decorator(
self, func=original_function, thread_id=thread_id, name=name, **kwargs
Expand All @@ -113,6 +173,21 @@ def step(
thread_id: Optional[str] = None,
**kwargs,
):
"""
Decorator or context manager for step-level event grouping.

Args:
original_function (callable, optional): Function to decorate. Defaults to None.
name (str, optional): Name of the step. Defaults to "".
type (TrueStepType, optional): Type of the step. Defaults to "undefined".
id (Optional[str], optional): ID of the step. Defaults to None.
parent_id (Optional[str], optional): Parent ID of the step. Defaults to None.
thread_id (Optional[str], optional): Thread ID of the step. Defaults to None.
**kwargs: Additional arguments for the step context manager or decorator.

Returns:
Union[StepContextManager, callable]: Step context manager or decorated function.
"""
if original_function:
return step_decorator(
self,
Expand Down Expand Up @@ -144,6 +219,19 @@ def run(
parent_id: Optional[str] = None,
thread_id: Optional[str] = None,
):
"""
Decorator or context manager for run-level event grouping.

Args:
original_function (callable, optional): Function to decorate. Defaults to None.
name (str, optional): Name of the run. Defaults to "".
id (Optional[str], optional): ID of the run. Defaults to None.
parent_id (Optional[str], optional): Parent ID of the run. Defaults to None.
thread_id (Optional[str], optional): Thread ID of the run. Defaults to None.

Returns:
Union[StepContextManager, callable]: Run context manager or decorated function.
"""
return self.step(
original_function=original_function,
name=name,
Expand All @@ -165,6 +253,23 @@ def message(
tags: Optional[List[str]] = None,
metadata: Dict = {},
):
"""
Creates and ends a message step.

Args:
content (str, optional): Content of the message. Defaults to "".
id (Optional[str], optional): ID of the message. Defaults to None.
parent_id (Optional[str], optional): Parent ID of the message. Defaults to None.
type (Optional[MessageStepType], optional): Type of the message. Defaults to None.
name (Optional[str], optional): Name of the message. Defaults to None.
thread_id (Optional[str], optional): Thread ID of the message. Defaults to None.
attachments (List[Attachment], optional): List of attachments. Defaults to [].
tags (Optional[List[str]], optional): List of tags. Defaults to None.
metadata (Dict, optional): Metadata for the message. Defaults to {}.

Returns:
Message: Created message step.
"""
step = Message(
name=name,
id=id,
Expand All @@ -190,6 +295,20 @@ def start_step(
thread_id: Optional[str] = None,
**kwargs,
):
"""
Starts a new step.

Args:
name (str, optional): Name of the step. Defaults to "".
type (Optional[TrueStepType], optional): Type of the step. Defaults to None.
id (Optional[str], optional): ID of the step. Defaults to None.
parent_id (Optional[str], optional): Parent ID of the step. Defaults to None.
thread_id (Optional[str], optional): Thread ID of the step. Defaults to None.
**kwargs: Additional arguments for the step.

Returns:
Step: Started step.
"""
step = Step(
name=name,
type=type,
Expand All @@ -203,24 +322,52 @@ def start_step(
return step

def get_current_step(self):
"""
Retrieves the current active step.

Returns:
Optional[Step]: The current active step or None if no steps are active.
"""
active_steps = active_steps_var.get()
if active_steps and len(active_steps) > 0:
return active_steps[-1]
else:
return None

def get_current_thread(self):
"""
Retrieves the current active thread.

Returns:
Optional[ThreadContextManager]: The current active thread or None if no threads are active.
"""
return active_thread_var.get()

def reset_context(self):
"""
Resets the active steps and thread context.
"""
active_steps_var.set([])
active_thread_var.set(None)

def flush_and_stop(self):
"""
Flushes the event processor and stops it.
"""
self.event_processor.flush_and_stop()


class LiteralClient(BaseLiteralClient):
"""
Synchronous LiteralClient for interacting with the Literal API.

Args:
batch_size (int, optional): Batch size for event processing. Defaults to 5.
api_key (Optional[str], optional): API key for authentication. If not provided, it will be fetched from the environment variable 'LITERAL_API_KEY'.
url (Optional[str], optional): URL of the Literal API. Defaults to 'https://cloud.getliteral.ai'.
disabled (bool, optional): Flag to disable the client. Defaults to False.
"""

api: LiteralAPI

def __init__(
Expand All @@ -239,10 +386,26 @@ def __init__(
)

def flush(self):
"""
Flushes the event processor.
"""
self.event_processor.flush()


class AsyncLiteralClient(BaseLiteralClient):
"""
Asynchronous LiteralClient for interacting with the Literal API.

Attributes:
api (AsyncLiteralAPI): Instance of the asynchronous Literal API.

Args:
batch_size (int, optional): Batch size for event processing. Defaults to 5.
api_key (Optional[str], optional): API key for authentication. If not provided, it will be fetched from the environment variable 'LITERAL_API_KEY'.
url (Optional[str], optional): URL of the Literal API. Defaults to 'https://cloud.getliteral.ai'.
disabled (bool, optional): Flag to disable the client. Defaults to False.
"""

api: AsyncLiteralAPI

def __init__(
Expand All @@ -261,4 +424,10 @@ def __init__(
)

async def flush(self):
await self.event_processor.aflush()
"""
Asynchronously flushes the event processor.

Returns:
None
"""
await self.event_processor.aflush()
Empty file modified literalai/context.py
100644 → 100755
Empty file.
Loading
Loading