langchain/libs/community/langchain_community/utilities/portkey.py

76 lines
2.3 KiB
Python
Raw Normal View History

community[major], core[patch], langchain[patch], experimental[patch]: Create langchain-community (#14463) Moved the following modules to new package langchain-community in a backwards compatible fashion: ``` mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community ``` Moved the following to core ``` mv langchain/langchain/utils/json_schema.py core/langchain_core/utils mv langchain/langchain/utils/html.py core/langchain_core/utils mv langchain/langchain/utils/strings.py core/langchain_core/utils cat langchain/langchain/utils/env.py >> core/langchain_core/utils/env.py rm langchain/langchain/utils/env.py ``` See .scripts/community_split/script_integrations.sh for all changes
2023-12-11 21:53:30 +00:00
import json
import os
from typing import Dict, Optional
class Portkey:
"""Portkey configuration.
Attributes:
base: The base URL for the Portkey API.
Default: "https://api.portkey.ai/v1/proxy"
"""
base = "https://api.portkey.ai/v1/proxy"
@staticmethod
def Config(
api_key: str,
trace_id: Optional[str] = None,
environment: Optional[str] = None,
user: Optional[str] = None,
organisation: Optional[str] = None,
prompt: Optional[str] = None,
retry_count: Optional[int] = None,
cache: Optional[str] = None,
cache_force_refresh: Optional[str] = None,
cache_age: Optional[int] = None,
) -> Dict[str, str]:
assert retry_count is None or retry_count in range(
1, 6
), "retry_count must be an integer and in range [1, 2, 3, 4, 5]"
assert cache is None or cache in [
"simple",
"semantic",
], "cache must be 'simple' or 'semantic'"
assert cache_force_refresh is None or (
isinstance(cache_force_refresh, str)
and cache_force_refresh in ["True", "False"]
), "cache_force_refresh must be 'True' or 'False'"
assert cache_age is None or isinstance(
cache_age, int
), "cache_age must be an integer"
os.environ["OPENAI_API_BASE"] = Portkey.base
headers = {
"x-portkey-api-key": api_key,
"x-portkey-mode": "proxy openai",
}
if trace_id:
headers["x-portkey-trace-id"] = trace_id
if retry_count:
headers["x-portkey-retry-count"] = str(retry_count)
if cache:
headers["x-portkey-cache"] = cache
if cache_force_refresh:
headers["x-portkey-cache-force-refresh"] = cache_force_refresh
if cache_age:
headers["Cache-Control"] = f"max-age:{str(cache_age)}"
metadata = {}
if environment:
metadata["_environment"] = environment
if user:
metadata["_user"] = user
if organisation:
metadata["_organisation"] = organisation
if prompt:
metadata["_prompt"] = prompt
if metadata:
headers.update({"x-portkey-metadata": json.dumps(metadata)})
return headers