mirror of
https://github.com/hwchase17/langchain
synced 2024-11-10 01:10:59 +00:00
ed58eeb9c5
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
55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
import json
|
|
from typing import Any, Callable, Dict, Optional
|
|
|
|
from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator
|
|
|
|
|
|
class GraphQLAPIWrapper(BaseModel):
|
|
"""Wrapper around GraphQL API.
|
|
|
|
To use, you should have the ``gql`` python package installed.
|
|
This wrapper will use the GraphQL API to conduct queries.
|
|
"""
|
|
|
|
custom_headers: Optional[Dict[str, str]] = None
|
|
graphql_endpoint: str
|
|
gql_client: Any #: :meta private:
|
|
gql_function: Callable[[str], Any] #: :meta private:
|
|
|
|
class Config:
|
|
"""Configuration for this pydantic object."""
|
|
|
|
extra = Extra.forbid
|
|
|
|
@root_validator(pre=True)
|
|
def validate_environment(cls, values: Dict) -> Dict:
|
|
"""Validate that the python package exists in the environment."""
|
|
try:
|
|
from gql import Client, gql
|
|
from gql.transport.requests import RequestsHTTPTransport
|
|
except ImportError as e:
|
|
raise ImportError(
|
|
"Could not import gql python package. "
|
|
f"Try installing it with `pip install gql`. Received error: {e}"
|
|
)
|
|
headers = values.get("custom_headers")
|
|
transport = RequestsHTTPTransport(
|
|
url=values["graphql_endpoint"],
|
|
headers=headers,
|
|
)
|
|
client = Client(transport=transport, fetch_schema_from_transport=True)
|
|
values["gql_client"] = client
|
|
values["gql_function"] = gql
|
|
return values
|
|
|
|
def run(self, query: str) -> str:
|
|
"""Run a GraphQL query and get the results."""
|
|
result = self._execute_query(query)
|
|
return json.dumps(result, indent=2)
|
|
|
|
def _execute_query(self, query: str) -> Dict[str, Any]:
|
|
"""Execute a GraphQL query and return the results."""
|
|
document_node = self.gql_function(query)
|
|
result = self.gql_client.execute(document_node)
|
|
return result
|