Skip to content

Commit

Permalink
import style (#1543)
Browse files Browse the repository at this point in the history
* adopt import style

* another file fixed

* more fixes

* fixing circular

* no aliases

* revert init

* revert another

* last few

* note -> warning

* more import styling

* another one

* more standardization

* more

* many more import style fixes

* fix typos

* update api dump

* remove private value

* remove private name from compat

* add more module renaming suggestions

* more module rename categories

* remove private

* typo

* another private removed

* update trulens_eval api dump

* docs typo fixes

* fix a few bad docs links
  • Loading branch information
sfc-gh-pmardziel authored Oct 15, 2024
1 parent 8cf02a8 commit d0fb82c
Show file tree
Hide file tree
Showing 161 changed files with 3,070 additions and 2,953 deletions.
14 changes: 6 additions & 8 deletions .grit/dealias_trulens_eval.grit
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ any {
`from trulens_eval.database.orm import new_base as $alias` => `from trulens.core.database.orm import new_base as $alias`,
`from trulens_eval.database.orm import new_orm` => `from trulens.core.database.orm import new_orm`,
`from trulens_eval.database.orm import new_orm as $alias` => `from trulens.core.database.orm import new_orm as $alias`,
`from trulens_eval.database.sqlalchemy import MIGRATION_UNKNOWN_STR` => `from trulens.core.database.sqlalchemy import MIGRATION_UNKNOWN_STR`,
`from trulens_eval.database.sqlalchemy import MIGRATION_UNKNOWN_STR as $alias` => `from trulens.core.database.sqlalchemy import MIGRATION_UNKNOWN_STR as $alias`,
`from trulens_eval.database.sqlalchemy import UNICODE_CHECK` => `from trulens.core.database.sqlalchemy import UNICODE_CHECK`,
`from trulens_eval.database.sqlalchemy import UNICODE_CHECK as $alias` => `from trulens.core.database.sqlalchemy import UNICODE_CHECK as $alias`,
`from trulens_eval.database.sqlalchemy import UNICODE_CLOCK` => `from trulens.core.database.sqlalchemy import UNICODE_CLOCK`,
Expand Down Expand Up @@ -184,8 +182,8 @@ any {
`from trulens_eval.feedback import imports_utils as $alias` => `from trulens.core.utils import imports as $alias`,
`from trulens_eval.feedback import Embeddings` => `from trulens.feedback.embeddings import Embeddings`,
`from trulens_eval.feedback import Embeddings as $alias` => `from trulens.feedback.embeddings import Embeddings as $alias`,
`from trulens_eval.feedback import Feedback` => `from trulens.feedback.feedback import Feedback`,
`from trulens_eval.feedback import Feedback as $alias` => `from trulens.feedback.feedback import Feedback as $alias`,
`from trulens_eval.feedback import Feedback` => `from trulens.core.feedback.feedback import Feedback`,
`from trulens_eval.feedback import Feedback as $alias` => `from trulens.core.feedback.feedback import Feedback as $alias`,
`from trulens_eval.feedback import GroundTruthAgreement` => `from trulens.feedback.groundtruth import GroundTruthAgreement`,
`from trulens_eval.feedback import GroundTruthAgreement as $alias` => `from trulens.feedback.groundtruth import GroundTruthAgreement as $alias`,
`from trulens_eval.feedback import LiteLLM` => `from trulens.providers.litellm.provider import LiteLLM`,
Expand All @@ -210,8 +208,8 @@ any {
`from trulens_eval.feedback.feedback import InvalidSelector as $alias` => `from trulens.core.feedback.feedback import InvalidSelector as $alias`,
`from trulens_eval.feedback.feedback import SkipEval` => `from trulens.core.feedback.feedback import SkipEval`,
`from trulens_eval.feedback.feedback import SkipEval as $alias` => `from trulens.core.feedback.feedback import SkipEval as $alias`,
`from trulens_eval.feedback.feedback import Feedback` => `from trulens.feedback.feedback import Feedback`,
`from trulens_eval.feedback.feedback import Feedback as $alias` => `from trulens.feedback.feedback import Feedback as $alias`,
`from trulens_eval.feedback.feedback import Feedback` => `from trulens.core.feedback.feedback import Feedback`,
`from trulens_eval.feedback.feedback import Feedback as $alias` => `from trulens.core.feedback.feedback import Feedback as $alias`,
`from trulens_eval.feedback.feedback import rag_triad` => `from trulens.feedback.feedback import rag_triad`,
`from trulens_eval.feedback.feedback import rag_triad as $alias` => `from trulens.feedback.feedback import rag_triad as $alias`,
`from trulens_eval.feedback.groundtruth import GroundTruthAgreement` => `from trulens.feedback.groundtruth import GroundTruthAgreement`,
Expand Down Expand Up @@ -346,8 +344,8 @@ any {
`from trulens_eval.feedback.provider.endpoint.base import EndpointCallback as $alias` => `from trulens.core.feedback.endpoint import EndpointCallback as $alias`,
`from trulens_eval.feedback.provider.endpoint.base import DummyEndpoint` => `from trulens.feedback.dummy.endpoint import DummyEndpoint`,
`from trulens_eval.feedback.provider.endpoint.base import DummyEndpoint as $alias` => `from trulens.feedback.dummy.endpoint import DummyEndpoint as $alias`,
`from trulens_eval.feedback.provider.endpoint.bedrock import INSTRUMENT` => `from trulens.providers.bedrock.endpoint import INSTRUMENT`,
`from trulens_eval.feedback.provider.endpoint.bedrock import INSTRUMENT as $alias` => `from trulens.providers.bedrock.endpoint import INSTRUMENT as $alias`,
`from trulens_eval.feedback.provider.endpoint.bedrock import INSTRUMENT` => `from trulens.core.feedback.endpoint import INSTRUMENT`,
`from trulens_eval.feedback.provider.endpoint.bedrock import INSTRUMENT as $alias` => `from trulens.core.feedback.endpoint import INSTRUMENT as $alias`,
`from trulens_eval.feedback.provider.endpoint.bedrock import BedrockCallback` => `from trulens.providers.bedrock.endpoint import BedrockCallback`,
`from trulens_eval.feedback.provider.endpoint.bedrock import BedrockCallback as $alias` => `from trulens.providers.bedrock.endpoint import BedrockCallback as $alias`,
`from trulens_eval.feedback.provider.endpoint.bedrock import BedrockEndpoint` => `from trulens.providers.bedrock.endpoint import BedrockEndpoint`,
Expand Down
6 changes: 3 additions & 3 deletions POLICIES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 📦 Release policies
# 📦 Release Policies

## Versioning

Expand Down Expand Up @@ -34,13 +34,13 @@ will occur at the introduction of the warning period.

- Starting 1.0, the `trulens_eval` package is being deprecated in favor of
`trulens` and several associated required and optional packages. See
[trulens_eval migration](/component_guides/other/trulens_eval_migration/) for details.
[trulens_eval migration](/component_guides/other/trulens_eval_migration) for details.

- Warning period: 2024-09-01 (`trulens-eval==1.0.1`) to 2024-10-14.
Backwards compatibility during the warning period is provided by the new
content of the `trulens_eval` package which provides aliases to the features
in their new locations. See
[trulens_eval](/reference/trulens_eval/index.md).
[trulens_eval](/reference/trulens_eval).

- Deprecated period: 2024-10-14 to 2025-12-01. Usage of `trulens_eval` will
produce errors indicating deprecation.
Expand Down
2 changes: 1 addition & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## 1.0.0

* Major package restructuring. See
<https://trulens.org/docs/trulens/guides/trulens_eval_migration> for details.
<https://www.trulens.org/component_guides/other/trulens_eval_migration/> for details.

## 0.33.0

Expand Down
2 changes: 1 addition & 1 deletion docs/blog/posts/release_blog_1dot.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ Along with the high level changes in TruLens v1, ground truth can now be persist
)
```

=== "Load and Evaluate with Persisted Groundtruth Data"
=== "Load and Evaluate with Persisted GroundTruth Data"

```python
from trulens.core import Feedback
Expand Down
2 changes: 1 addition & 1 deletion docs/component_guides/evaluation/feedback_providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ Feedback functions in common across these providers are in their abstract class

## Provider Combinations

- [Groundtruth][trulens.feedback.groundtruth.GroundTruthAgreement]
- [GroundTruth][trulens.feedback.groundtruth.GroundTruthAgreement]
2 changes: 1 addition & 1 deletion docs/contributing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ See [Development guide](development.md).
The design of the API is governed by the principles outlined in the
[Design](design.md) doc.

### Release Policies
### 📦 Release Policies

Versioning and deprecation guidelines are included. [Release policies](policies.md).

Expand Down
40 changes: 35 additions & 5 deletions docs/contributing/standards.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ apply when referring to things like package names, classes, methods.

```python
from trulens.schema.record import Record # don't do this
from trulens.schema import record as mod_record # do this instead
from trulens.schema import record as record_schema # do this instead
```

This prevents the `record` module from being loaded until something inside it
is needed. If your uses of `mod_record.Record` are inside functions, this
is needed. If your uses of `record_schema.Record` are inside functions, this
loading can be delayed as far as the execution of that function.

- Import and rename modules:
Expand All @@ -61,16 +61,46 @@ apply when referring to things like package names, classes, methods.
This is especially important for module names which might cause name
collisions with other things such as variables named `record`.

- Keep module renames consistent:
- Keep module renames consistent using the following patterns:

```python
# schema
from trulens.schema import X as X_schema
from trulens.utils import X as X_utils

# utils
from trulens.utils import X as X_utils # if X was plural, make X singular in rename

# providers
from trulens.providers.X import provider as X_provider
from trulens.providers.X import endpoint as X_endpoint

# apps
from trulens.apps.X import Y as Y_app

# connectors
from trulens.connector import X as X_connector

# core modules
from trulens.core import X as core_X

# core.feedback modules
from trulens.core.feedback import X as core_X

# core.database modules
from trulens.core.database import base as core_db
from trulens.core.database import connector as core_connector
from trulens.core.database import X as X_db

# dashboard modules
from trulens.dashboard.X import Y as dashboard_Y

# if X is inside some category of module Y:
from trulens...Y import Y as X_Y
from trulens...Y import X as X_Y
# otherwise if X is not in some category of modules:
from trulens... import X as mod_X

# Some modules do not need renaming:
from trulens.feedback import llm_provider
```

- If an imported module is only used in type annotations, import it inside a
Expand Down
11 changes: 6 additions & 5 deletions examples/dev/dummy_app/agent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Any, Dict, Optional

from trulens.apps.custom import TruCustomApp
from trulens.apps.custom import instrument
from trulens.apps import custom as custom_app

from examples.dev.dummy_app.dummy import Dummy

Expand Down Expand Up @@ -47,11 +46,13 @@ def __init__(
self.app = app

if self.use_app:
self.tru_app = TruCustomApp(self.app, app_name=description)
self.tru_app = custom_app.TruCustomApp(
self.app, app_name=description
)

self.dummy_allocate()

@instrument
@custom_app.instrument
def invoke(self, data: str) -> str:
"""Invoke the dummy tool."""

Expand All @@ -69,7 +70,7 @@ def invoke(self, data: str) -> str:
else:
return "Record placeholder"

@instrument
@custom_app.instrument
async def ainvoke(self, data: str) -> str:
"""Invoke the dummy tool."""

Expand Down
6 changes: 3 additions & 3 deletions examples/dev/dummy_app/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
from typing import Optional

from trulens.core.utils.python import OpaqueWrapper
from trulens.core.utils import python as python_utils


class Dummy:
Expand Down Expand Up @@ -53,10 +53,10 @@ async def dummy_await(self, delay: Optional[float] = None):
def dummy_allocate(self):
"""Allocate some memory."""

self._dummy_allocated_data = OpaqueWrapper(
self._dummy_allocated_data = python_utils.OpaqueWrapper(
obj=[True] * self.alloc, e=Exception()
)
# OpaqueWrapper will prevent instrumentation or serialization of the
# python_utils.OpaqueWrapper will prevent instrumentation or serialization of the
# contents of this fake data.

return self._dummy_allocated_data
9 changes: 4 additions & 5 deletions examples/dev/dummy_app/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from typing import Callable, List, Optional

from trulens.apps.custom import instrument
from trulens.core.utils.python import WeakWrapper
from trulens.core.utils.python import superstack
from trulens.core.utils import python as python_utils

from examples.dev.dummy_app.dummy import Dummy

Expand Down Expand Up @@ -64,7 +63,7 @@ async def ainvoke(self, data: str):
class DummyStackTool(DummyTool):
"""A tool that returns a rendering of the call stack when it is invoked."""

last_stacks: List[WeakWrapper] = []
last_stacks: List[python_utils.WeakWrapper] = []
"""The stacks seen during save_stack invocations.
You can use this to get the readout even if this tool is used deep in an app
Expand All @@ -83,10 +82,10 @@ def save_stack(self, data: str) -> str:
# stack to include in the return of this method but at the same time
# want to be able to take a look at those things which we didn't
# serialize.
current_stack = list(superstack())
current_stack = list(python_utils.superstack())
DummyStackTool.last_stacks.append(
# Has to be a weakref to prevent GC test failures.
WeakWrapper(current_stack)
python_utils.WeakWrapper(current_stack)
)

ret = "<table>\n"
Expand Down
20 changes: 12 additions & 8 deletions examples/experimental/end2end_apps/streamlit_app/app.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
from dotenv import load_dotenv
from langchain_openai import OpenAI
import streamlit as st
from trulens.apps.langchain import TruChain
from trulens.core import Feedback
from trulens.core import TruSession
from trulens.apps.langchain import tru_chain as mod_tru_chain
from trulens.core import session as core_session
from trulens.core.feedback import feedback as core_feedback
import trulens.dashboard.streamlit as trulens_st
from trulens.providers.openai import OpenAI as fOpenAI
from trulens.providers.openai import provider as openai_provider

load_dotenv()

session = TruSession()
session = core_session.TruSession()

st.title("🦑 Using TruLens Components in Streamlit")

provider = fOpenAI()
provider = openai_provider.OpenAI()

f_coherence = Feedback(provider.coherence_with_cot_reasons).on_output()
f_coherence = core_feedback.Feedback(
provider.coherence_with_cot_reasons
).on_output()

feedbacks = [f_coherence]


def generate_response(input_text):
llm = OpenAI(temperature=0.7)
tru_llm = TruChain(llm, app_name="LLM v1", feedbacks=feedbacks)
tru_llm = mod_tru_chain.TruChain(
llm, app_name="LLM v1", feedbacks=feedbacks
)
with tru_llm as recording:
response = llm.invoke(input_text)
record = recording.get()
Expand Down
40 changes: 21 additions & 19 deletions examples/experimental/end2end_apps/trubot/App_TruBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
import numpy as np
import pinecone
import streamlit as st
from trulens.apps.langchain import TruChain
from trulens.core import Feedback
from trulens.core import Select
from trulens.core import TruSession
from trulens.core.utils.keys import check_keys
from trulens.providers.huggingface import Huggingface
from trulens.providers.openai import OpenAI as fOpenAI
from trulens.apps.langchain import tru_chain as mod_tru_chain
from trulens.core import session as core_session
from trulens.core.feedback import feedback as core_feedback
from trulens.core.schema import select as select_schema
from trulens.core.utils import keys as key_utils
from trulens.providers.huggingface import provider as huggingface_provider
from trulens.providers.openai import provider as openai_provider

check_keys("PINECONE_API_KEY", "PINECONE_ENV", "OPENAI_API_KEY")
key_utils.check_keys("PINECONE_API_KEY", "PINECONE_ENV", "OPENAI_API_KEY")

# Set up GPT-3 model
model_name = "gpt-3.5-turbo"
Expand All @@ -38,22 +38,24 @@

identity = lambda h: h

hugs = Huggingface()
openai = fOpenAI()
hugs = huggingface_provider.Huggingface()
openai = openai_provider.OpenAI()

f_lang_match = Feedback(hugs.language_match).on(
text1=Select.RecordInput, text2=Select.RecordOutput
f_lang_match = core_feedback.Feedback(hugs.language_match).on(
text1=select_schema.Select.RecordInput,
text2=select_schema.Select.RecordOutput,
)

f_qa_relevance = Feedback(openai.relevance).on(
prompt=Select.RecordInput, response=Select.RecordOutput
f_qa_relevance = core_feedback.Feedback(openai.relevance).on(
prompt=select_schema.Select.RecordInput,
response=select_schema.Select.RecordOutput,
)

f_context_relevance = (
Feedback(openai.context_relevance)
core_feedback.Feedback(openai.context_relevance)
.on(
question=Select.RecordInput,
statement=Select.Record.chain.combine_docs_chain._call.args.inputs.input_documents[
question=select_schema.Select.RecordInput,
statement=select_schema.Select.Record.chain.combine_docs_chain._call.args.inputs.input_documents[
:
].page_content,
)
Expand Down Expand Up @@ -125,7 +127,7 @@ def generate_response(prompt):
)

# Trulens instrumentation.
tc = TruChain(chain, app_name=app_name)
tc = mod_tru_chain.TruChain(chain, app_name=app_name)

return tc, tc.with_record(dict(question=prompt))

Expand All @@ -149,7 +151,7 @@ def generate_response(prompt):
# Display response
st.write(answer)

session = TruSession()
session = core_session.TruSession()
record_id = session.add_data(
app_name=app_name,
prompt=prompt_input,
Expand Down
Loading

0 comments on commit d0fb82c

Please sign in to comment.