|
1 | | -import logging |
2 | | -import os |
3 | | -from os import environ as env |
4 | | -from typing import Generator, Optional |
5 | | - |
6 | | -import httpx |
7 | | -import pytest |
8 | | -from langchain.embeddings import CacheBackedEmbeddings |
9 | | -from langchain.globals import set_llm_cache |
10 | | -from langchain.storage import LocalFileStore |
11 | | -from langchain_community.cache import SQLiteCache |
12 | | - |
13 | | -from uipath_langchain.embeddings import UiPathOpenAIEmbeddings |
14 | | -from uipath_langchain.utils._settings import UiPathCachedPathsSettings |
15 | | - |
16 | | -test_cache_settings = UiPathCachedPathsSettings( |
17 | | - CACHED_COMPLETION_DB="tests/llm_cache/tests_uipath_cache.sqlite", |
18 | | - CACHED_EMBEDDINGS_DIR="tests/llm_cache/cached_embeddings", |
19 | | -) |
20 | | - |
21 | | - |
22 | | -def get_from_uipath_url(): |
23 | | - try: |
24 | | - url = os.getenv("UIPATH_URL") |
25 | | - if url: |
26 | | - return "/".join(url.split("/", 3)[:3]) |
27 | | - except Exception: |
28 | | - return None |
29 | | - return None |
30 | | - |
31 | | - |
32 | | -def get_token(): |
33 | | - url_get_token = f"{get_from_uipath_url().rstrip('/')}/identity_/connect/token" |
34 | | - |
35 | | - os.environ["UIPATH_REQUESTING_PRODUCT"] = "uipath-python-sdk" |
36 | | - os.environ["UIPATH_REQUESTING_FEATURE"] = "langgraph-agent" |
37 | | - os.environ["UIPATH_TESTS_CACHE_LLMGW"] = "true" |
38 | | - |
39 | | - token_credentials = { |
40 | | - "client_id": env.get("UIPATH_CLIENT_ID"), |
41 | | - "client_secret": env.get("UIPATH_CLIENT_SECRET"), |
42 | | - "grant_type": "client_credentials", |
43 | | - } |
44 | | - |
45 | | - try: |
46 | | - with httpx.Client() as client: |
47 | | - response = client.post(url_get_token, data=token_credentials) |
48 | | - response.raise_for_status() |
49 | | - res_json = response.json() |
50 | | - token = res_json.get("access_token") |
51 | | - |
52 | | - if not token: |
53 | | - pytest.skip("Authentication token is empty or missing") |
54 | | - except (httpx.HTTPError, ValueError, KeyError) as e: |
55 | | - pytest.skip(f"Failed to obtain authentication token: {str(e)}") |
56 | | - |
57 | | - return token |
58 | | - |
59 | | - |
60 | | -@pytest.fixture(autouse=True) |
61 | | -def setup_test_env(): |
62 | | - env["UIPATH_ACCESS_TOKEN"] = get_token() |
63 | | - |
64 | | - |
65 | | -@pytest.fixture(scope="session") |
66 | | -def cached_llmgw_calls() -> Generator[Optional[SQLiteCache], None, None]: |
67 | | - if not os.environ.get("UIPATH_TESTS_CACHE_LLMGW"): |
68 | | - yield None |
69 | | - else: |
70 | | - logging.info("Setting up LLMGW cache") |
71 | | - db_path = test_cache_settings.cached_completion_db |
72 | | - os.makedirs(os.path.dirname(db_path), exist_ok=True) |
73 | | - cache = SQLiteCache(database_path=db_path) |
74 | | - set_llm_cache(cache) |
75 | | - yield cache |
76 | | - set_llm_cache(None) |
77 | | - return |
78 | | - |
79 | | - |
80 | | -@pytest.fixture(scope="session") |
81 | | -def cached_embedder() -> Generator[Optional[CacheBackedEmbeddings], None, None]: |
82 | | - if not os.environ.get("UIPATH_TESTS_CACHE_LLMGW"): |
83 | | - yield None |
84 | | - else: |
85 | | - logging.info("Setting up embeddings cache") |
86 | | - model = "text-embedding-3-large" |
87 | | - embedder = CacheBackedEmbeddings.from_bytes_store( |
88 | | - underlying_embeddings=UiPathOpenAIEmbeddings(model=model), |
89 | | - document_embedding_cache=LocalFileStore( |
90 | | - test_cache_settings.cached_embeddings_dir |
91 | | - ), |
92 | | - namespace=model, |
93 | | - ) |
94 | | - yield embedder |
95 | | - return |
| 1 | +# import logging |
| 2 | +# import os |
| 3 | +# from os import environ as env |
| 4 | +# from typing import Generator, Optional |
| 5 | + |
| 6 | +# import httpx |
| 7 | +# import pytest |
| 8 | +# from langchain.embeddings import CacheBackedEmbeddings |
| 9 | +# from langchain.globals import set_llm_cache |
| 10 | +# from langchain.storage import LocalFileStore |
| 11 | +# from langchain_community.cache import SQLiteCache |
| 12 | + |
| 13 | +# from uipath_langchain.embeddings import UiPathOpenAIEmbeddings |
| 14 | +# from uipath_langchain.utils._settings import UiPathCachedPathsSettings |
| 15 | + |
| 16 | +# test_cache_settings = UiPathCachedPathsSettings( |
| 17 | +# CACHED_COMPLETION_DB="tests/llm_cache/tests_uipath_cache.sqlite", |
| 18 | +# CACHED_EMBEDDINGS_DIR="tests/llm_cache/cached_embeddings", |
| 19 | +# ) |
| 20 | + |
| 21 | + |
| 22 | +# def get_from_uipath_url(): |
| 23 | +# try: |
| 24 | +# url = os.getenv("UIPATH_URL", "https://cloud.uipath.com/dummyOrg/dummyTennant/") |
| 25 | +# if url: |
| 26 | +# return "/".join(url.split("/", 3)[:3]) |
| 27 | +# except Exception: |
| 28 | +# return "https://cloud.uipath.com/dummyOrg/dummyTennant/" |
| 29 | +# return None |
| 30 | + |
| 31 | + |
| 32 | +# def get_token(): |
| 33 | +# url_get_token = f"{get_from_uipath_url().rstrip('/')}/identity_/connect/token" |
| 34 | + |
| 35 | +# os.environ["UIPATH_REQUESTING_PRODUCT"] = "uipath-python-sdk" |
| 36 | +# os.environ["UIPATH_REQUESTING_FEATURE"] = "langgraph-agent" |
| 37 | +# os.environ["UIPATH_TESTS_CACHE_LLMGW"] = "true" |
| 38 | + |
| 39 | +# token_credentials = { |
| 40 | +# "client_id": env.get("UIPATH_CLIENT_ID"), |
| 41 | +# "client_secret": env.get("UIPATH_CLIENT_SECRET"), |
| 42 | +# "grant_type": "client_credentials", |
| 43 | +# } |
| 44 | + |
| 45 | +# try: |
| 46 | +# with httpx.Client() as client: |
| 47 | +# response = client.post(url_get_token, data=token_credentials) |
| 48 | +# response.raise_for_status() |
| 49 | +# res_json = response.json() |
| 50 | +# token = res_json.get("access_token") |
| 51 | + |
| 52 | +# if not token: |
| 53 | +# pytest.skip("Authentication token is empty or missing") |
| 54 | +# except (httpx.HTTPError, ValueError, KeyError) as e: |
| 55 | +# pytest.skip(f"Failed to obtain authentication token: {str(e)}") |
| 56 | + |
| 57 | +# return token |
| 58 | + |
| 59 | + |
| 60 | +# @pytest.fixture(autouse=True) |
| 61 | +# def setup_test_env(): |
| 62 | +# env["UIPATH_ACCESS_TOKEN"] = get_token() |
| 63 | + |
| 64 | + |
| 65 | +# @pytest.fixture(scope="session") |
| 66 | +# def cached_llmgw_calls() -> Generator[Optional[SQLiteCache], None, None]: |
| 67 | +# if not os.environ.get("UIPATH_TESTS_CACHE_LLMGW"): |
| 68 | +# yield None |
| 69 | +# else: |
| 70 | +# logging.info("Setting up LLMGW cache") |
| 71 | +# db_path = test_cache_settings.cached_completion_db |
| 72 | +# os.makedirs(os.path.dirname(db_path), exist_ok=True) |
| 73 | +# cache = SQLiteCache(database_path=db_path) |
| 74 | +# set_llm_cache(cache) |
| 75 | +# yield cache |
| 76 | +# set_llm_cache(None) |
| 77 | +# return |
| 78 | + |
| 79 | + |
| 80 | +# @pytest.fixture(scope="session") |
| 81 | +# def cached_embedder() -> Generator[Optional[CacheBackedEmbeddings], None, None]: |
| 82 | +# if not os.environ.get("UIPATH_TESTS_CACHE_LLMGW"): |
| 83 | +# yield None |
| 84 | +# else: |
| 85 | +# logging.info("Setting up embeddings cache") |
| 86 | +# model = "text-embedding-3-large" |
| 87 | +# embedder = CacheBackedEmbeddings.from_bytes_store( |
| 88 | +# underlying_embeddings=UiPathOpenAIEmbeddings(model=model), |
| 89 | +# document_embedding_cache=LocalFileStore( |
| 90 | +# test_cache_settings.cached_embeddings_dir |
| 91 | +# ), |
| 92 | +# namespace=model, |
| 93 | +# ) |
| 94 | +# yield embedder |
| 95 | +# return |
0 commit comments