method_name
stringlengths
1
78
method_body
stringlengths
3
9.66k
full_code
stringlengths
31
10.7k
docstring
stringlengths
4
4.74k
test_confluence_loader_load_data_by_page_ids
mock_confluence.get_page_by_id.side_effect = [self._get_mock_page('123'), self._get_mock_page('456')] mock_confluence.get_all_restrictions_for_content.side_effect = [self. _get_mock_page_restrictions('123'), self._get_mock_page_restrictions('456') ] confluence_loader = self._get_mock_confluence_loader(mock_...
def test_confluence_loader_load_data_by_page_ids(self, mock_confluence: MagicMock) ->None: mock_confluence.get_page_by_id.side_effect = [self._get_mock_page('123' ), self._get_mock_page('456')] mock_confluence.get_all_restrictions_for_content.side_effect = [self. _get_mock_page_restrictions(...
null
lookup_with_id_through_llm
llm_string = get_prompts({**llm.dict(), **{'stop': stop}}, [])[1] return self.lookup_with_id(prompt, llm_string=llm_string)
def lookup_with_id_through_llm(self, prompt: str, llm: LLM, stop: Optional[ List[str]]=None) ->Optional[Tuple[str, RETURN_VAL_TYPE]]: llm_string = get_prompts({**llm.dict(), **{'stop': stop}}, [])[1] return self.lookup_with_id(prompt, llm_string=llm_string)
null
_llm_type
"""Return type of chat model.""" return 'mlflow-ai-gateway-chat'
@property def _llm_type(self) ->str: """Return type of chat model.""" return 'mlflow-ai-gateway-chat'
Return type of chat model.
plan
"""Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with the observations. callbacks: Callbacks to run. **kwargs: User inputs. Returns: Action specifying what tool to use. """ outpu...
def plan(self, intermediate_steps: List[Tuple[AgentAction, str]], callbacks: Callbacks=None, **kwargs: Any) ->Union[AgentAction, AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with the observations. ...
Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with the observations. callbacks: Callbacks to run. **kwargs: User inputs. Returns: Action specifying what tool to use.
test_with_types_with_type_generics
"""Verify that with_types works if we use things like List[int]""" def foo(x: int) ->None: """Add one to the input.""" raise NotImplementedError() RunnableLambda(foo).with_types(output_type=List[int], input_type=List[int]) RunnableLambda(foo).with_types(output_type=Sequence[int], input_type= Sequence[int])
def test_with_types_with_type_generics() ->None: """Verify that with_types works if we use things like List[int]""" def foo(x: int) ->None: """Add one to the input.""" raise NotImplementedError() RunnableLambda(foo).with_types(output_type=List[int], input_type=List[int]) RunnableLambda(...
Verify that with_types works if we use things like List[int]
visit_operation
args = [arg.accept(self) for arg in operation.arguments] return {self._format_func(operation.operator): args}
def visit_operation(self, operation: Operation) ->Dict: args = [arg.accept(self) for arg in operation.arguments] return {self._format_func(operation.operator): args}
null
lazy_load
"""Lazy load text from the url(s) in web_path.""" for path in self.web_paths: soup = self._scrape(path, bs_kwargs=self.bs_kwargs) text = soup.get_text(**self.bs_get_text_kwargs) metadata = _build_metadata(soup, path) yield Document(page_content=text, metadata=metadata)
def lazy_load(self) ->Iterator[Document]: """Lazy load text from the url(s) in web_path.""" for path in self.web_paths: soup = self._scrape(path, bs_kwargs=self.bs_kwargs) text = soup.get_text(**self.bs_get_text_kwargs) metadata = _build_metadata(soup, path) yield Document(page_c...
Lazy load text from the url(s) in web_path.
__init__
"""Initializes the loader. Args: config: The config to pass to the source connector. stream_name: The name of the stream to load. record_handler: A function that takes in a record and an optional id and returns a Document. If None, the record will be used as ...
def __init__(self, config: Mapping[str, Any], stream_name: str, record_handler: Optional[RecordHandler]=None, state: Optional[Any]=None ) ->None: """Initializes the loader. Args: config: The config to pass to the source connector. stream_name: The name of the stream to load....
Initializes the loader. Args: config: The config to pass to the source connector. stream_name: The name of the stream to load. record_handler: A function that takes in a record and an optional id and returns a Document. If None, the record will be used as the document. Defaults to None. ...
test_sql_database_run
"""Test that commands can be run successfully and returned in correct format.""" engine = create_engine('sqlite:///:memory:') metadata_obj.create_all(engine) stmt = insert(user).values(user_id=13, user_name='Harrison', user_bio= 'That is my Bio ' * 24) with engine.begin() as conn: conn.execute(stmt) db = SQLDat...
def test_sql_database_run() ->None: """Test that commands can be run successfully and returned in correct format.""" engine = create_engine('sqlite:///:memory:') metadata_obj.create_all(engine) stmt = insert(user).values(user_id=13, user_name='Harrison', user_bio= 'That is my Bio ' * 24) wit...
Test that commands can be run successfully and returned in correct format.
refresh_schema
"""Refreshes the schema of the FalkorDB database""" node_properties: List[Any] = self.query(node_properties_query) rel_properties: List[Any] = self.query(rel_properties_query) relationships: List[Any] = self.query(rel_query) self.structured_schema = {'node_props': {el[0]['label']: el[0]['keys'] for el in node_prope...
def refresh_schema(self) ->None: """Refreshes the schema of the FalkorDB database""" node_properties: List[Any] = self.query(node_properties_query) rel_properties: List[Any] = self.query(rel_properties_query) relationships: List[Any] = self.query(rel_query) self.structured_schema = {'node_props': {e...
Refreshes the schema of the FalkorDB database
external_import_error
if name == 'streamlit.external.langchain': raise ImportError return self.builtins_import(name, globals, locals, fromlist, level)
def external_import_error(name: str, globals: Any, locals: Any, fromlist: Any, level: int) ->Any: if name == 'streamlit.external.langchain': raise ImportError return self.builtins_import(name, globals, locals, fromlist, level)
null
equals
if not isinstance(other, type(self)): return False return self._field == other._field and self._value == other._value
def equals(self, other: 'RedisFilterField') ->bool: if not isinstance(other, type(self)): return False return self._field == other._field and self._value == other._value
null
__init__
self.mlflow = import_mlflow() if 'DATABRICKS_RUNTIME_VERSION' in os.environ: self.mlflow.set_tracking_uri('databricks') self.mlf_expid = self.mlflow.tracking.fluent._get_experiment_id() self.mlf_exp = self.mlflow.get_experiment(self.mlf_expid) else: tracking_uri = get_from_dict_or_env(kwargs, 'tracking_...
def __init__(self, **kwargs: Any): self.mlflow = import_mlflow() if 'DATABRICKS_RUNTIME_VERSION' in os.environ: self.mlflow.set_tracking_uri('databricks') self.mlf_expid = self.mlflow.tracking.fluent._get_experiment_id() self.mlf_exp = self.mlflow.get_experiment(self.mlf_expid) else:...
null
test_default_w_embeddings_on
llm, PROMPT = setup() feature_embedder = pick_best_chain.PickBestFeatureEmbedder(auto_embed=True, model=MockEncoderReturnsList()) chain = pick_best_chain.PickBest.from_llm(llm=llm, prompt=PROMPT, feature_embedder=feature_embedder, auto_embed=True) str1 = '0' str2 = '1' ctx_str_1 = 'context1' dot_prod = 'dotprod...
@pytest.mark.requires('vowpal_wabbit_next', 'sentence_transformers') def test_default_w_embeddings_on() ->None: llm, PROMPT = setup() feature_embedder = pick_best_chain.PickBestFeatureEmbedder(auto_embed= True, model=MockEncoderReturnsList()) chain = pick_best_chain.PickBest.from_llm(llm=llm, prompt...
null
embed_documents
"""Embed a list of documents using EdenAI. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """ return self._generate_embeddings(texts)
def embed_documents(self, texts: List[str]) ->List[List[float]]: """Embed a list of documents using EdenAI. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """ return self._generate_embeddings(texts)
Embed a list of documents using EdenAI. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text.
embed_documents
""" Make a list of texts into a list of embedding vectors. """ return [self.embed_query(text) for text in texts]
def embed_documents(self, texts: List[str]) ->List[List[float]]: """ Make a list of texts into a list of embedding vectors. """ return [self.embed_query(text) for text in texts]
Make a list of texts into a list of embedding vectors.
_generate
if self.streaming: raise ValueError('`streaming` option currently unsupported.') if not self.access_token: self._refresh_access_token_with_lock() payload = {'messages': [_convert_message_to_dict(m) for m in messages], 'top_p': self.top_p, 'temperature': self.temperature, 'penalty_score': self.penalty_sc...
def _generate(self, messages: List[BaseMessage], stop: Optional[List[str]]= None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any ) ->ChatResult: if self.streaming: raise ValueError('`streaming` option currently unsupported.') if not self.access_token: self._refresh_a...
null
embed_query
"""Return simple embeddings.""" return [float(1.0)] * (OS_TOKEN_COUNT - 1) + [float(texts.index(text))]
def embed_query(self, text: str) ->List[float]: """Return simple embeddings.""" return [float(1.0)] * (OS_TOKEN_COUNT - 1) + [float(texts.index(text))]
Return simple embeddings.
test_similarity_search_approx_with_hybrid_search
"""Test end to end construction and search with metadata.""" texts = ['foo', 'bar', 'baz'] docsearch = ElasticsearchStore.from_texts(texts, FakeEmbeddings(), ** elasticsearch_connection, index_name=index_name, strategy= ElasticsearchStore.ApproxRetrievalStrategy(hybrid=True)) def assert_query(query_body: dict, ...
def test_similarity_search_approx_with_hybrid_search(self, elasticsearch_connection: dict, index_name: str) ->None: """Test end to end construction and search with metadata.""" texts = ['foo', 'bar', 'baz'] docsearch = ElasticsearchStore.from_texts(texts, FakeEmbeddings(), ** elasticsearch_conne...
Test end to end construction and search with metadata.
test_max_marginal_relevance_search
"""Test MRR search.""" metadatas = [{'page': i} for i in range(len(texts))] docsearch = DocArrayHnswSearch.from_texts(texts, FakeEmbeddings(), metadatas=metadatas, dist_metric=metric, work_dir=str(tmp_path), n_dim=10) output = docsearch.max_marginal_relevance_search('foo', k=2, fetch_k=3) assert output == [Document...
@pytest.mark.parametrize('metric', ['cosine', 'l2']) def test_max_marginal_relevance_search(metric: str, texts: List[str], tmp_path: Path) ->None: """Test MRR search.""" metadatas = [{'page': i} for i in range(len(texts))] docsearch = DocArrayHnswSearch.from_texts(texts, FakeEmbeddings(), metada...
Test MRR search.
clear
"""Clear session memory from DB""" self.collection.delete_many(filter={'session_id': self.session_id})
def clear(self) ->None: """Clear session memory from DB""" self.collection.delete_many(filter={'session_id': self.session_id})
Clear session memory from DB
test_debug_is_settable_directly
from langchain_core.callbacks.manager import _get_debug import langchain previous_value = langchain.debug previous_fn_reading = _get_debug() assert previous_value == previous_fn_reading langchain.debug = not previous_value new_value = langchain.debug new_fn_reading = _get_debug() try: assert new_value != previous_v...
def test_debug_is_settable_directly() ->None: from langchain_core.callbacks.manager import _get_debug import langchain previous_value = langchain.debug previous_fn_reading = _get_debug() assert previous_value == previous_fn_reading langchain.debug = not previous_value new_value = langchain.d...
null
test_invoke
"""Test invoke tokens from ChatAnthropicMessages.""" llm = ChatAnthropicMessages(model_name='claude-instant-1.2') result = llm.invoke("I'm Pickle Rick", config=dict(tags=['foo'])) assert isinstance(result.content, str)
def test_invoke() ->None: """Test invoke tokens from ChatAnthropicMessages.""" llm = ChatAnthropicMessages(model_name='claude-instant-1.2') result = llm.invoke("I'm Pickle Rick", config=dict(tags=['foo'])) assert isinstance(result.content, str)
Test invoke tokens from ChatAnthropicMessages.
load
"""Load given path as pages.""" return list(self.lazy_load())
def load(self) ->List[Document]: """Load given path as pages.""" return list(self.lazy_load())
Load given path as pages.
_create_retry_decorator
"""Returns a tenacity retry decorator, preconfigured to handle PaLM exceptions""" import google.api_core.exceptions multiplier = 2 min_seconds = 1 max_seconds = 60 max_retries = 10 return retry(reraise=True, stop=stop_after_attempt(max_retries), wait= wait_exponential(multiplier=multiplier, min=min_seconds, max= ...
def _create_retry_decorator() ->Callable[[Any], Any]: """Returns a tenacity retry decorator, preconfigured to handle PaLM exceptions""" import google.api_core.exceptions multiplier = 2 min_seconds = 1 max_seconds = 60 max_retries = 10 return retry(reraise=True, stop=stop_after_attempt(max_re...
Returns a tenacity retry decorator, preconfigured to handle PaLM exceptions
_similarity_search_with_relevance_scores
"""Return docs and relevance scores, normalized on a scale from 0 to 1. 0 is dissimilar, 1 is most similar. """ raise NotImplementedError()
def _similarity_search_with_relevance_scores(self, query: str, k: int=4, ** kwargs: Any) ->List[Tuple[Document, float]]: """Return docs and relevance scores, normalized on a scale from 0 to 1. 0 is dissimilar, 1 is most similar. """ raise NotImplementedError()
Return docs and relevance scores, normalized on a scale from 0 to 1. 0 is dissimilar, 1 is most similar.
__init__
"""Initialize with domain, access_token (tenant / user), and document_id. Args: domain: The domain to load the LarkSuite. access_token: The access_token to use. document_id: The document_id to load. """ self.domain = domain self.access_token = access_token self.docum...
def __init__(self, domain: str, access_token: str, document_id: str): """Initialize with domain, access_token (tenant / user), and document_id. Args: domain: The domain to load the LarkSuite. access_token: The access_token to use. document_id: The document_id to load. ...
Initialize with domain, access_token (tenant / user), and document_id. Args: domain: The domain to load the LarkSuite. access_token: The access_token to use. document_id: The document_id to load.
flush_tracker
"""Flush the tracker and reset the session. Args: repo (:obj:`str`, optional): Aim repository path or Repo object to which Run object is bound. If skipped, default Repo is used. experiment_name (:obj:`str`, optional): Sets Run's `experiment` property. 'de...
def flush_tracker(self, repo: Optional[str]=None, experiment_name: Optional [str]=None, system_tracking_interval: Optional[int]=10, log_system_params: bool=True, langchain_asset: Any=None, reset: bool= True, finish: bool=False) ->None: """Flush the tracker and reset the session. Args: ...
Flush the tracker and reset the session. Args: repo (:obj:`str`, optional): Aim repository path or Repo object to which Run object is bound. If skipped, default Repo is used. experiment_name (:obj:`str`, optional): Sets Run's `experiment` property. 'default' if not specified. Can be used later ...
_llm_type
"""Return type of llm.""" return 'deepsparse'
@property def _llm_type(self) ->str: """Return type of llm.""" return 'deepsparse'
Return type of llm.
_result_to_document
main_meta = {'title': outline_res['document']['title'], 'source': self. outline_instance_url + outline_res['document']['url']} add_meta = {'id': outline_res['document']['id'], 'ranking': outline_res[ 'ranking'], 'collection_id': outline_res['document']['collectionId'], 'parent_document_id': outline_res['doc...
def _result_to_document(self, outline_res: Any) ->Document: main_meta = {'title': outline_res['document']['title'], 'source': self. outline_instance_url + outline_res['document']['url']} add_meta = {'id': outline_res['document']['id'], 'ranking': outline_res ['ranking'], 'collection_id': outline...
null
_build_llm_df
base_df_fields = [field for field in base_df_fields if field in base_df] rename_map = {map_entry_k: map_entry_v for map_entry_k, map_entry_v in rename_map.items() if map_entry_k in base_df_fields} llm_df = base_df[base_df_fields].dropna(axis=1) if rename_map: llm_df = llm_df.rename(rename_map, axis=1) return ll...
@staticmethod def _build_llm_df(base_df: pd.DataFrame, base_df_fields: Sequence, rename_map: Mapping) ->pd.DataFrame: base_df_fields = [field for field in base_df_fields if field in base_df] rename_map = {map_entry_k: map_entry_v for map_entry_k, map_entry_v in rename_map.items() if map_entry_k in b...
null
load_suggestions
"""Load suggestions. Args: query: A query string doc_type: The type of document to search for. Can be one of "all", "device", "guide", "teardown", "answer", "wiki". Returns: """ res = requests.get(IFIXIT_BASE_URL + '/suggest/' + query + '?doctypes=' + ...
@staticmethod def load_suggestions(query: str='', doc_type: str='all') ->List[Document]: """Load suggestions. Args: query: A query string doc_type: The type of document to search for. Can be one of "all", "device", "guide", "teardown", "answer", "wiki". Return...
Load suggestions. Args: query: A query string doc_type: The type of document to search for. Can be one of "all", "device", "guide", "teardown", "answer", "wiki". Returns:
setup_class
assert os.getenv('XATA_API_KEY' ), 'XATA_API_KEY environment variable is not set' assert os.getenv('XATA_DB_URL'), 'XATA_DB_URL environment variable is not set'
@classmethod def setup_class(cls) ->None: assert os.getenv('XATA_API_KEY' ), 'XATA_API_KEY environment variable is not set' assert os.getenv('XATA_DB_URL' ), 'XATA_DB_URL environment variable is not set'
null
test_load_full_confluence_space
loader = ConfluenceLoader(url='https://templates.atlassian.net/wiki/') docs = loader.load(space_key='RD') assert len(docs) == 14 assert docs[0].page_content is not None
@pytest.mark.skipif(not confluence_installed, reason= 'Atlassian package not installed') def test_load_full_confluence_space() ->None: loader = ConfluenceLoader(url='https://templates.atlassian.net/wiki/') docs = loader.load(space_key='RD') assert len(docs) == 14 assert docs[0].page_content is not N...
null
similarity_search_with_relevance_scores
"""Perform a similarity search with ClickHouse Args: query (str): query string k (int, optional): Top K neighbors to retrieve. Defaults to 4. where_str (Optional[str], optional): where condition string. Defaults to None. ...
def similarity_search_with_relevance_scores(self, query: str, k: int=4, where_str: Optional[str]=None, **kwargs: Any) ->List[Tuple[Document, float] ]: """Perform a similarity search with ClickHouse Args: query (str): query string k (int, optional): Top K neighbors to retriev...
Perform a similarity search with ClickHouse Args: query (str): query string k (int, optional): Top K neighbors to retrieve. Defaults to 4. where_str (Optional[str], optional): where condition string. Defaults to None. NOTE: Please do not let end-user to fill th...
_call
_run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager() _table_names = self.sql_chain.database.get_usable_table_names() table_names = ', '.join(_table_names) llm_inputs = {'query': inputs[self.input_key], 'table_names': table_names} _lowercased_table_names = [name.lower() for name in _table_names] t...
def _call(self, inputs: Dict[str, Any], run_manager: Optional[ CallbackManagerForChainRun]=None) ->Dict[str, Any]: _run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager() _table_names = self.sql_chain.database.get_usable_table_names() table_names = ', '.join(_table_names) llm_inp...
null
input_keys
"""Expect url and browser content. :meta private: """ return [self.input_url_key, self.input_browser_content_key]
@property def input_keys(self) ->List[str]: """Expect url and browser content. :meta private: """ return [self.input_url_key, self.input_browser_content_key]
Expect url and browser content. :meta private:
stream
yield from self.transform(iter([input]), config)
def stream(self, input: Input, config: Optional[RunnableConfig]=None, ** kwargs: Optional[Any]) ->Iterator[Dict[str, Any]]: yield from self.transform(iter([input]), config)
null
ignore_agent
"""Whether to ignore agent callbacks.""" return self.ignore_agent_
@property def ignore_agent(self) ->bool: """Whether to ignore agent callbacks.""" return self.ignore_agent_
Whether to ignore agent callbacks.
_call
"""Call out to Aleph Alpha's completion endpoint. Args: prompt: The prompt to pass into the model. stop: Optional list of stop words to use when generating. Returns: The string generated by the model. Example: .. code-block:: python ...
def _call(self, prompt: str, stop: Optional[List[str]]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->str: """Call out to Aleph Alpha's completion endpoint. Args: prompt: The prompt to pass into the model. stop: Optional list of stop words to use wh...
Call out to Aleph Alpha's completion endpoint. Args: prompt: The prompt to pass into the model. stop: Optional list of stop words to use when generating. Returns: The string generated by the model. Example: .. code-block:: python response = aleph_alpha("Tell me a joke.")
test_python_repl_tool_single_input
"""Test that the python REPL tool works with a single input.""" tool = PythonREPLTool() assert tool.is_single_input assert int(tool.run('print(1 + 1)').strip()) == 2
def test_python_repl_tool_single_input() ->None: """Test that the python REPL tool works with a single input.""" tool = PythonREPLTool() assert tool.is_single_input assert int(tool.run('print(1 + 1)').strip()) == 2
Test that the python REPL tool works with a single input.
_format_tweets
"""Format tweets into a string.""" for tweet in tweets: metadata = {'created_at': tweet['created_at'], 'user_info': user_info} yield Document(page_content=tweet['text'], metadata=metadata)
def _format_tweets(self, tweets: List[Dict[str, Any]], user_info: dict ) ->Iterable[Document]: """Format tweets into a string.""" for tweet in tweets: metadata = {'created_at': tweet['created_at'], 'user_info': user_info} yield Document(page_content=tweet['text'], metadata=metadata)
Format tweets into a string.
__init__
self.value = value self.keep = keep
def __init__(self, value: Any, keep: bool=False): self.value = value self.keep = keep
null
test_tracer_chain_run
"""Test tracer on a Chain run.""" uuid = uuid4() compare_run = Run(id=str(uuid), start_time=datetime.now(timezone.utc), end_time=datetime.now(timezone.utc), events=[{'name': 'start', 'time': datetime.now(timezone.utc)}, {'name': 'end', 'time': datetime.now( timezone.utc)}], extra={}, execution_order=1, chil...
@freeze_time('2023-01-01') def test_tracer_chain_run() ->None: """Test tracer on a Chain run.""" uuid = uuid4() compare_run = Run(id=str(uuid), start_time=datetime.now(timezone.utc), end_time=datetime.now(timezone.utc), events=[{'name': 'start', 'time': datetime.now(timezone.utc)}, {'name': ...
Test tracer on a Chain run.
test__collapse_docs_one_doc
"""Test collapse documents functionality when only one document present.""" docs = [Document(page_content='foo')] output = collapse_docs(docs, _fake_combine_docs_func) assert output == docs[0] docs = [Document(page_content='foo', metadata={'source': 'a'})] output = collapse_docs(docs, _fake_combine_docs_func) assert ou...
def test__collapse_docs_one_doc() ->None: """Test collapse documents functionality when only one document present.""" docs = [Document(page_content='foo')] output = collapse_docs(docs, _fake_combine_docs_func) assert output == docs[0] docs = [Document(page_content='foo', metadata={'source': 'a'})] ...
Test collapse documents functionality when only one document present.
create
models = importlib.import_module('langchain.chat_models') model_cls = getattr(models, provider) model_config = model_cls(**kwargs) converted_messages = convert_openai_messages(messages) if not stream: result = model_config.invoke(converted_messages) return ChatCompletions(choices=[Choice(message=convert_message...
@staticmethod def create(messages: Sequence[Dict[str, Any]], *, provider: str= 'ChatOpenAI', stream: bool=False, **kwargs: Any) ->Union[ ChatCompletions, Iterable]: models = importlib.import_module('langchain.chat_models') model_cls = getattr(models, provider) model_config = model_cls(**kwargs) ...
null
_create_chat_result
generations = [] for res in response.choices: message = convert_dict_to_message({'role': 'assistant', 'content': res. text}) gen = ChatGeneration(message=message, generation_info=dict( finish_reason=res.finish_reason)) generations.append(gen) llm_output = {'token_usage': response.meta, 'mode...
def _create_chat_result(self, response: GenerationResponse) ->ChatResult: generations = [] for res in response.choices: message = convert_dict_to_message({'role': 'assistant', 'content': res.text}) gen = ChatGeneration(message=message, generation_info=dict( finish_reason=...
null
test_chat_bedrock_generate
"""Test BedrockChat wrapper with generate.""" message = HumanMessage(content='Hello') response = chat.generate([[message], [message]]) assert isinstance(response, LLMResult) assert len(response.generations) == 2 for generations in response.generations: for generation in generations: assert isinstance(genera...
@pytest.mark.scheduled def test_chat_bedrock_generate(chat: BedrockChat) ->None: """Test BedrockChat wrapper with generate.""" message = HumanMessage(content='Hello') response = chat.generate([[message], [message]]) assert isinstance(response, LLMResult) assert len(response.generations) == 2 for...
Test BedrockChat wrapper with generate.
test_serialization
"""Test serialization.""" from langchain.chains.loading import load_chain with TemporaryDirectory() as temp_dir: file = temp_dir + '/llm.json' fake_llm_chain.save(file) loaded_chain = load_chain(file) assert loaded_chain == fake_llm_chain
@patch('langchain_community.llms.loading.get_type_to_cls_dict', lambda : { 'fake': lambda : FakeLLM}) def test_serialization(fake_llm_chain: LLMChain) ->None: """Test serialization.""" from langchain.chains.loading import load_chain with TemporaryDirectory() as temp_dir: file = temp_dir + '/llm....
Test serialization.
test_openlm_call
"""Test valid call to openlm.""" llm = OpenLM(model_name='dolly-v2-7b', max_tokens=10) output = llm(prompt='Say foo:') assert isinstance(output, str)
def test_openlm_call() ->None: """Test valid call to openlm.""" llm = OpenLM(model_name='dolly-v2-7b', max_tokens=10) output = llm(prompt='Say foo:') assert isinstance(output, str)
Test valid call to openlm.
test_character_text_splitter_keep_separator_regex
"""Test splitting by characters while keeping the separator that is a regex special character. """ text = 'foo.bar.baz.123' splitter = CharacterTextSplitter(separator=separator, chunk_size=1, chunk_overlap=0, keep_separator=True, is_separator_regex=is_separator_regex ) output = splitter.split_text(text)...
@pytest.mark.parametrize('separator, is_separator_regex', [(re.escape('.'), True), ('.', False)]) def test_character_text_splitter_keep_separator_regex(separator: str, is_separator_regex: bool) ->None: """Test splitting by characters while keeping the separator that is a regex special character. """...
Test splitting by characters while keeping the separator that is a regex special character.
test_escaping_lucene
"""Test escaping lucene characters""" assert remove_lucene_chars('Hello+World') == 'Hello World' assert remove_lucene_chars('Hello World\\') == 'Hello World' assert remove_lucene_chars('It is the end of the world. Take shelter!' ) == 'It is the end of the world. Take shelter' assert remove_lucene_chars('It is the e...
def test_escaping_lucene() ->None: """Test escaping lucene characters""" assert remove_lucene_chars('Hello+World') == 'Hello World' assert remove_lucene_chars('Hello World\\') == 'Hello World' assert remove_lucene_chars('It is the end of the world. Take shelter!' ) == 'It is the end of the world...
Test escaping lucene characters
stop_cb
"""callback that stop continuous recognition""" speech_recognizer.stop_continuous_recognition_async() nonlocal done done = True
def stop_cb(evt: Any) ->None: """callback that stop continuous recognition""" speech_recognizer.stop_continuous_recognition_async() nonlocal done done = True
callback that stop continuous recognition
test_graph_cypher_qa_chain_prompt_selection_5
qa_prompt_template = 'QA Prompt' cypher_prompt_template = 'Cypher Prompt' memory = ConversationBufferMemory(memory_key='chat_history') readonlymemory = ReadOnlySharedMemory(memory=memory) qa_prompt = PromptTemplate(template=qa_prompt_template, input_variables=[]) cypher_prompt = PromptTemplate(template=cypher_prompt_te...
def test_graph_cypher_qa_chain_prompt_selection_5() ->None: qa_prompt_template = 'QA Prompt' cypher_prompt_template = 'Cypher Prompt' memory = ConversationBufferMemory(memory_key='chat_history') readonlymemory = ReadOnlySharedMemory(memory=memory) qa_prompt = PromptTemplate(template=qa_prompt_templa...
null
_get_relevant_documents
""" Get the relevant documents for a given query. Args: query: The query to search for. Returns: A list of reranked documents. """ fused_documents = self.rank_fusion(query, run_manager) return fused_documents
def _get_relevant_documents(self, query: str, *, run_manager: CallbackManagerForRetrieverRun) ->List[Document]: """ Get the relevant documents for a given query. Args: query: The query to search for. Returns: A list of reranked documents. """ fused_d...
Get the relevant documents for a given query. Args: query: The query to search for. Returns: A list of reranked documents.
_similarity_search_with_relevance_scores
docs_dists = self.similarity_search_with_score(query, k=k, **kwargs) docs, dists = zip(*docs_dists) scores = [(1 / math.exp(dist)) for dist in dists] return list(zip(list(docs), scores))
def _similarity_search_with_relevance_scores(self, query: str, k: int= DEFAULT_K, **kwargs: Any) ->List[Tuple[Document, float]]: docs_dists = self.similarity_search_with_score(query, k=k, **kwargs) docs, dists = zip(*docs_dists) scores = [(1 / math.exp(dist)) for dist in dists] return list(zip(list(...
null
_import_spark_sql_tool_BaseSparkSQLTool
from langchain_community.tools.spark_sql.tool import BaseSparkSQLTool return BaseSparkSQLTool
def _import_spark_sql_tool_BaseSparkSQLTool() ->Any: from langchain_community.tools.spark_sql.tool import BaseSparkSQLTool return BaseSparkSQLTool
null
_evaluate_agent_trajectory
"""Evaluate a trajectory. Args: prediction (str): The final predicted response. agent_trajectory (List[Tuple[AgentAction, str]]): The intermediate steps forming the agent trajectory. input (str): The input to the agent. reference (Optional[str]): ...
@abstractmethod def _evaluate_agent_trajectory(self, *, prediction: str, agent_trajectory: Sequence[Tuple[AgentAction, str]], input: str, reference: Optional[str] =None, **kwargs: Any) ->dict: """Evaluate a trajectory. Args: prediction (str): The final predicted response. ag...
Evaluate a trajectory. Args: prediction (str): The final predicted response. agent_trajectory (List[Tuple[AgentAction, str]]): The intermediate steps forming the agent trajectory. input (str): The input to the agent. reference (Optional[str]): The reference answer. Returns: dict: The evalu...
_model_default_factory
try: from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline except ImportError as e: raise ImportError( 'Cannot import transformers, please install with `pip install transformers`.' ) from e tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForS...
def _model_default_factory(model_name: str= 'laiyer/deberta-v3-base-prompt-injection') ->Pipeline: try: from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline except ImportError as e: raise ImportError( 'Cannot import transformers, please install wit...
null
from_documents
texts = [d.page_content for d in documents] metadatas = [d.metadata for d in documents] return cls.from_texts(texts, embedding, metadatas, index_name, content_key, metadata_key, **kwargs)
@classmethod def from_documents(cls, documents: List[Document], embedding: Embeddings, metadatas: Optional[List[dict]]=None, index_name: str='langchain', content_key: str='content', metadata_key: str='metadata', **kwargs: Any ) ->Tair: texts = [d.page_content for d in documents] metadatas = [d.metad...
null
__init__
"""Initialize a PALValidation instance. Args: solution_expression_name (str): Name of the expected solution expression. If passed, solution_expression_type must be passed as well. solution_expression_type (str): AST type of the expected solution expressio...
def __init__(self, solution_expression_name: Optional[str]=None, solution_expression_type: Optional[type]=None, allow_imports: bool= False, allow_command_exec: bool=False): """Initialize a PALValidation instance. Args: solution_expression_name (str): Name of the expected solution expres...
Initialize a PALValidation instance. Args: solution_expression_name (str): Name of the expected solution expression. If passed, solution_expression_type must be passed as well. solution_expression_type (str): AST type of the expected solution expression. If passed, solution_expression_name must...
_load_prompt
return create_prompt(num_plates=request.num_plates, num_rows=request. num_rows, num_cols=request.num_cols)
def _load_prompt(request: FileProcessingRequest): return create_prompt(num_plates=request.num_plates, num_rows=request. num_rows, num_cols=request.num_cols)
null
test_placeholder
"""Used for compiling integration tests without running any real tests.""" pass
@pytest.mark.compile def test_placeholder() ->None: """Used for compiling integration tests without running any real tests.""" pass
Used for compiling integration tests without running any real tests.
validate_search_type
"""Validate search type.""" if 'search_type' in values: search_type = values['search_type'] if search_type not in ('similarity', 'hybrid', 'semantic_hybrid'): raise ValueError(f'search_type of {search_type} not allowed.') return values
@root_validator() def validate_search_type(cls, values: Dict) ->Dict: """Validate search type.""" if 'search_type' in values: search_type = values['search_type'] if search_type not in ('similarity', 'hybrid', 'semantic_hybrid'): raise ValueError(f'search_type of {search_type} not all...
Validate search type.
_collection_exists
"""Checks whether a collection exists for this message history""" try: self.client.Collections.get(collection=self.collection) except self.rockset.exceptions.NotFoundException: return False return True
def _collection_exists(self) ->bool: """Checks whether a collection exists for this message history""" try: self.client.Collections.get(collection=self.collection) except self.rockset.exceptions.NotFoundException: return False return True
Checks whether a collection exists for this message history
_import_json_tool_JsonGetValueTool
from langchain_community.tools.json.tool import JsonGetValueTool return JsonGetValueTool
def _import_json_tool_JsonGetValueTool() ->Any: from langchain_community.tools.json.tool import JsonGetValueTool return JsonGetValueTool
null
parse_filename
"""Parse the filename from an url. Args: url: Url to parse the filename from. Returns: The filename. """ if (url_path := Path(url)) and url_path.suffix == '.pdf': return url_path.name else: return self._parse_filename_from_url(url)
def parse_filename(self, url: str) ->str: """Parse the filename from an url. Args: url: Url to parse the filename from. Returns: The filename. """ if (url_path := Path(url)) and url_path.suffix == '.pdf': return url_path.name else: return sel...
Parse the filename from an url. Args: url: Url to parse the filename from. Returns: The filename.
embeddings
return self.embedding_function
@property def embeddings(self) ->Embeddings: return self.embedding_function
null
test_openai_invalid_model_kwargs
with pytest.raises(ValueError): OpenAIEmbeddings(model_kwargs={'model': 'foo'})
@pytest.mark.requires('openai') def test_openai_invalid_model_kwargs() ->None: with pytest.raises(ValueError): OpenAIEmbeddings(model_kwargs={'model': 'foo'})
null
json
return {'status': 'ok', 'payload': base64.b64encode(bytes( '{"some": "data"}}', 'utf-8'))}
def json(self) ->Any: return {'status': 'ok', 'payload': base64.b64encode(bytes( '{"some": "data"}}', 'utf-8'))}
null
run
"""Run query through Searx API and parse results. You can pass any other params to the searx query API. Args: query: The query to search for. query_suffix: Extra suffix appended to the query. engines: List of engines to use for the query. categories: Lis...
def run(self, query: str, engines: Optional[List[str]]=None, categories: Optional[List[str]]=None, query_suffix: Optional[str]='', **kwargs: Any ) ->str: """Run query through Searx API and parse results. You can pass any other params to the searx query API. Args: query: The que...
Run query through Searx API and parse results. You can pass any other params to the searx query API. Args: query: The query to search for. query_suffix: Extra suffix appended to the query. engines: List of engines to use for the query. categories: List of categories to use for the query. **kwargs:...
llm_dataset_name
import pandas as pd client = Client() df = pd.DataFrame({'input': ["What's the capital of California?", "What's the capital of Nevada?", "What's the capital of Oregon?", "What's the capital of Washington?"], 'output': ['Sacramento', 'Carson City', 'Salem', 'Olympia']}) uid = str(uuid4())[-8:] _dataset_name ...
@pytest.fixture(scope='module') def llm_dataset_name() ->Iterator[str]: import pandas as pd client = Client() df = pd.DataFrame({'input': ["What's the capital of California?", "What's the capital of Nevada?", "What's the capital of Oregon?", "What's the capital of Washington?"], 'output': ['...
null
on_agent_finish
"""Do nothing"""
def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) ->None: """Do nothing"""
Do nothing
_call
jsonformer = import_jsonformer() from transformers import Text2TextGenerationPipeline pipeline = cast(Text2TextGenerationPipeline, self.pipeline) model = jsonformer.Jsonformer(model=pipeline.model, tokenizer=pipeline. tokenizer, json_schema=self.json_schema, prompt=prompt, max_number_tokens=self.max_new_tokens,...
def _call(self, prompt: str, stop: Optional[List[str]]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->str: jsonformer = import_jsonformer() from transformers import Text2TextGenerationPipeline pipeline = cast(Text2TextGenerationPipeline, self.pipeline) model = jsonforme...
null
_import_faiss
from langchain_community.vectorstores.faiss import FAISS return FAISS
def _import_faiss() ->Any: from langchain_community.vectorstores.faiss import FAISS return FAISS
null
search_api
"""Search the API for the query.""" assert isinstance(query, str) return 'API result'
@tool(return_direct=True) def search_api(query: str) ->str: """Search the API for the query.""" assert isinstance(query, str) return 'API result'
Search the API for the query.
key
"""Construct the record key to use""" return self.key_prefix + self.session_id
@property def key(self) ->str: """Construct the record key to use""" return self.key_prefix + self.session_id
Construct the record key to use
visit_operation
args = [arg.accept(self) for arg in operation.arguments] return {self._format_func(operation.operator): args}
def visit_operation(self, operation: Operation) ->Dict: args = [arg.accept(self) for arg in operation.arguments] return {self._format_func(operation.operator): args}
null
get_excerpt
if self.AdditionalAttributes and self.AdditionalAttributes[0 ].Key == 'AnswerText': excerpt = self.get_attribute_value() elif self.DocumentExcerpt: excerpt = self.DocumentExcerpt.Text else: excerpt = '' return excerpt
def get_excerpt(self) ->str: if self.AdditionalAttributes and self.AdditionalAttributes[0 ].Key == 'AnswerText': excerpt = self.get_attribute_value() elif self.DocumentExcerpt: excerpt = self.DocumentExcerpt.Text else: excerpt = '' return excerpt
null
_parseMeta
filepath = '' if filecol == '': nvec = list(nvmap.keys()) vvec = list(nvmap.values()) else: nvec = [] vvec = [] if filecol in nvmap: nvec.append(filecol) vvec.append(nvmap[filecol]) filepath = nvmap[filecol] for k, v in nvmap.items(): if k != filecol: ...
def _parseMeta(self, nvmap: dict, filecol: str) ->Tuple[List[str], List[str ], str]: filepath = '' if filecol == '': nvec = list(nvmap.keys()) vvec = list(nvmap.values()) else: nvec = [] vvec = [] if filecol in nvmap: nvec.append(filecol) v...
null
get_input_schema
return create_model('AnalyzeDocumentChain', **{self.input_key: (str, None)})
def get_input_schema(self, config: Optional[RunnableConfig]=None) ->Type[ BaseModel]: return create_model('AnalyzeDocumentChain', **{self.input_key: (str, None)} )
null
_get_relevant_documents
"""Get relevated documents given a user question. Args: query: user question Returns: Relevant documents for re-phrased question """ response = self.llm_chain(query, callbacks=run_manager.get_child()) re_phrased_question = response['text'] logger.info(f'Re-phrased quest...
def _get_relevant_documents(self, query: str, *, run_manager: CallbackManagerForRetrieverRun) ->List[Document]: """Get relevated documents given a user question. Args: query: user question Returns: Relevant documents for re-phrased question """ response = se...
Get relevated documents given a user question. Args: query: user question Returns: Relevant documents for re-phrased question
ParseFromString
self.uuid = 'fake_uuid'
def ParseFromString(self: Any, data: str) ->None: self.uuid = 'fake_uuid'
null
_get_source_id_assigner
"""Get the source id from the document.""" if source_id_key is None: return lambda doc: None elif isinstance(source_id_key, str): return lambda doc: doc.metadata[source_id_key] elif callable(source_id_key): return source_id_key else: raise ValueError( f'source_id_key should be either None, a str...
def _get_source_id_assigner(source_id_key: Union[str, Callable[[Document], str], None]) ->Callable[[Document], Union[str, None]]: """Get the source id from the document.""" if source_id_key is None: return lambda doc: None elif isinstance(source_id_key, str): return lambda doc: doc.metad...
Get the source id from the document.
test_functions_call_thoughts
chat = QianfanChatEndpoint(model='ERNIE-Bot') prompt_tmpl = 'Use the given functions to answer following question: {input}' prompt_msgs = [HumanMessagePromptTemplate.from_template(prompt_tmpl)] prompt = ChatPromptTemplate(messages=prompt_msgs) chain = prompt | chat.bind(functions=_FUNCTIONS) message = HumanMessage(cont...
def test_functions_call_thoughts() ->None: chat = QianfanChatEndpoint(model='ERNIE-Bot') prompt_tmpl = ( 'Use the given functions to answer following question: {input}') prompt_msgs = [HumanMessagePromptTemplate.from_template(prompt_tmpl)] prompt = ChatPromptTemplate(messages=prompt_msgs) ch...
null
print_task_list
print('\x1b[95m\x1b[1m' + """ *****TASK LIST***** """ + '\x1b[0m\x1b[0m') for t in self.task_list: print(str(t['task_id']) + ': ' + t['task_name'])
def print_task_list(self) ->None: print('\x1b[95m\x1b[1m' + '\n*****TASK LIST*****\n' + '\x1b[0m\x1b[0m') for t in self.task_list: print(str(t['task_id']) + ': ' + t['task_name'])
null
__init__
"""Initialize the LangChain tracer.""" super().__init__(**kwargs) self.example_id = UUID(example_id) if isinstance(example_id, str ) else example_id self.project_name = project_name or ls_utils.get_tracer_project() self.client = client or get_client() self._futures: weakref.WeakSet[Future] = weakref.WeakSet() self....
def __init__(self, example_id: Optional[Union[UUID, str]]=None, project_name: Optional[str]=None, client: Optional[Client]=None, tags: Optional[List[str]]=None, use_threading: bool=True, **kwargs: Any) ->None: """Initialize the LangChain tracer.""" super().__init__(**kwargs) self.example_id = UUID(e...
Initialize the LangChain tracer.
test_prompt_with_chat_model
prompt = SystemMessagePromptTemplate.from_template('You are a nice assistant.' ) + '{question}' chat = FakeListChatModel(responses=['foo']) chain: Runnable = prompt | chat assert repr(chain) == snapshot assert isinstance(chain, RunnableSequence) assert chain.first == prompt assert chain.middle == [] assert chain.la...
@freeze_time('2023-01-01') def test_prompt_with_chat_model(mocker: MockerFixture, snapshot: SnapshotAssertion) ->None: prompt = SystemMessagePromptTemplate.from_template( 'You are a nice assistant.') + '{question}' chat = FakeListChatModel(responses=['foo']) chain: Runnable = prompt | chat a...
null
get_summaries_as_docs
return self.client.get_summaries_as_docs(self.query)
def get_summaries_as_docs(self) ->List[Document]: return self.client.get_summaries_as_docs(self.query)
null
_import_stackexchange
from langchain_community.utilities.stackexchange import StackExchangeAPIWrapper return StackExchangeAPIWrapper
def _import_stackexchange() ->Any: from langchain_community.utilities.stackexchange import StackExchangeAPIWrapper return StackExchangeAPIWrapper
null
_convert_date
return datetime.fromtimestamp(date / 1000).strftime('%Y-%m-%d %H:%M:%S')
def _convert_date(self, date: int) ->str: return datetime.fromtimestamp(date / 1000).strftime('%Y-%m-%d %H:%M:%S')
null
load_deanonymizer_mapping
"""Load the deanonymizer mapping from a JSON or YAML file. Args: file_path: Path to file to load the mapping from. Example: .. code-block:: python anonymizer.load_deanonymizer_mapping(file_path="path/mapping.json") """ load_path = Path(file_path) if load_path.s...
def load_deanonymizer_mapping(self, file_path: Union[Path, str]) ->None: """Load the deanonymizer mapping from a JSON or YAML file. Args: file_path: Path to file to load the mapping from. Example: .. code-block:: python anonymizer.load_deanonymizer_mapping(file_pat...
Load the deanonymizer mapping from a JSON or YAML file. Args: file_path: Path to file to load the mapping from. Example: .. code-block:: python anonymizer.load_deanonymizer_mapping(file_path="path/mapping.json")
get_type_to_cls_dict
return {'ai21': _import_ai21, 'aleph_alpha': _import_aleph_alpha, 'amazon_api_gateway': _import_amazon_api_gateway, 'amazon_bedrock': _import_bedrock, 'anthropic': _import_anthropic, 'anyscale': _import_anyscale, 'arcee': _import_arcee, 'aviary': _import_aviary, 'azure': _import_azure_openai, 'azureml_e...
def get_type_to_cls_dict() ->Dict[str, Callable[[], Type[BaseLLM]]]: return {'ai21': _import_ai21, 'aleph_alpha': _import_aleph_alpha, 'amazon_api_gateway': _import_amazon_api_gateway, 'amazon_bedrock': _import_bedrock, 'anthropic': _import_anthropic, 'anyscale': _import_anyscale, 'arcee': _...
null
__init__
"""Initializes the selector.""" self.examples = examples
def __init__(self, examples: Sequence[Dict[str, str]]) ->None: """Initializes the selector.""" self.examples = examples
Initializes the selector.
load_chain_from_config
"""Load chain from Config Dict.""" if '_type' not in config: raise ValueError('Must specify a chain Type in config') config_type = config.pop('_type') if config_type not in type_to_loader_dict: raise ValueError(f'Loading {config_type} chain not supported') chain_loader = type_to_loader_dict[config_type] return ...
def load_chain_from_config(config: dict, **kwargs: Any) ->Chain: """Load chain from Config Dict.""" if '_type' not in config: raise ValueError('Must specify a chain Type in config') config_type = config.pop('_type') if config_type not in type_to_loader_dict: raise ValueError(f'Loading {c...
Load chain from Config Dict.
test_index_specification_generation
index_schema = {'text': [{'name': 'job'}, {'name': 'title'}], 'numeric': [{ 'name': 'salary'}]} text = ['foo'] meta = {'job': 'engineer', 'title': 'principal engineer', 'salary': 100000} docs = [Document(page_content=t, metadata=meta) for t in text] r = Redis.from_documents(docs, FakeEmbeddings(), redis_url=TEST_RE...
def test_index_specification_generation() ->None: index_schema = {'text': [{'name': 'job'}, {'name': 'title'}], 'numeric': [{'name': 'salary'}]} text = ['foo'] meta = {'job': 'engineer', 'title': 'principal engineer', 'salary': 100000} docs = [Document(page_content=t, metadata=meta) for t in tex...
null
test_facebook_chat_loader
"""Test FacebookChatLoader.""" file_path = Path(__file__).parent.parent / 'examples/facebook_chat.json' loader = FacebookChatLoader(str(file_path)) docs = loader.load() assert len(docs) == 1 assert docs[0].metadata['source'] == str(file_path) assert docs[0].page_content == """User 2 on 2023-02-05 13:46:11: Bye! User 1...
def test_facebook_chat_loader() ->None: """Test FacebookChatLoader.""" file_path = Path(__file__).parent.parent / 'examples/facebook_chat.json' loader = FacebookChatLoader(str(file_path)) docs = loader.load() assert len(docs) == 1 assert docs[0].metadata['source'] == str(file_path) assert do...
Test FacebookChatLoader.
test_google_generativeai_generate
n = 1 if model_name == 'gemini-pro' else 2 llm = GoogleGenerativeAI(temperature=0.3, n=n, model=model_name) output = llm.generate(['Say foo:']) assert isinstance(output, LLMResult) assert len(output.generations) == 1 assert len(output.generations[0]) == n
@pytest.mark.parametrize('model_name', model_names) def test_google_generativeai_generate(model_name: str) ->None: n = 1 if model_name == 'gemini-pro' else 2 llm = GoogleGenerativeAI(temperature=0.3, n=n, model=model_name) output = llm.generate(['Say foo:']) assert isinstance(output, LLMResult) asse...
null
format
"""Format the chat template into a string. Args: **kwargs: keyword arguments to use for filling in template variables in all the template messages in this chat template. Returns: formatted string """ return self.format_prompt(**kwargs).to_string()
def format(self, **kwargs: Any) ->str: """Format the chat template into a string. Args: **kwargs: keyword arguments to use for filling in template variables in all the template messages in this chat template. Returns: formatted string """ r...
Format the chat template into a string. Args: **kwargs: keyword arguments to use for filling in template variables in all the template messages in this chat template. Returns: formatted string
_create_chat_result
generations = [] if not isinstance(response, dict): response = response.dict() for res in response['choices']: message = convert_dict_to_message(res['message']) generation_info = dict(finish_reason=res.get('finish_reason')) if 'logprobs' in res: generation_info['logprobs'] = res['logprobs'] ...
def _create_chat_result(self, response: Union[dict, BaseModel]) ->ChatResult: generations = [] if not isinstance(response, dict): response = response.dict() for res in response['choices']: message = convert_dict_to_message(res['message']) generation_info = dict(finish_reason=res.get(...
null