from langchain_core.messages import HumanMessage, SystemMessage
chat.invoke( [ HumanMessage( content="Translate this sentence from English to French: I love programming." ) ] )
AIMessage(content=‘J’aime la programmation.\n\nIn this translation, “I love” is translated to “J’aime” and "
programming" is translated to “la programmation.” The sentence structure remains the same as in English, with the
subject (I) followed by the verb (love) and then the object (programming).’, response_metadata={‘token_usage’:
{‘completion_tokens’: 68, ‘prompt_tokens’: 15, ‘total_tokens’: 83}, ‘model_name’: ‘moonshot-v1-8k’, ’
system_fingerprint’: None, ‘finish_reason’: ‘stop’, ‘logprobs’: None})
messages = [ SystemMessage( content="You are a helpful assistant that translates English to French." ), HumanMessage(content="I love programming."), ] chat(messages)
batch_messages = [ [ SystemMessage( content="You are a helpful assistant that translates English to chinese." ), HumanMessage(content="I love programming."), ], [ SystemMessage( content="You are a helpful assistant that translates English to chinese." ), HumanMessage(content="I love artificial intelligence."), ], ] result = chat.generate(batch_messages) # 在Python中,当你想要在控制台中输出变量的值时,通常会使用 print 函数。但是,如果你在交互式环境(比如Python解释器或Jupyter Notebook)中执行这段代码,并且想要查看 result 的值,你可以直接输入变量名 result,而不需要使用 print 函数。 result
# Caching 缓存 # LangChain为聊天模型提供了一个可选的缓存层。这很有用,原因有两个: # 如果您经常多次请求相同的完成,它可以通过减少您向LLM提供商进行的 API 调用次数来为您节省资金。它可以通过减少您对LLM提供程序进行的 API 调用次数来加快应用程序的速度。
# In Memory Cache 内存缓存中
# <!-- ruff: noqa: F821 --> from langchain.globalsimport set_llm_cache
# %time from langchain.cache import InMemoryCache
set_llm_cache(InMemoryCache())
# The first time, it is not yet in cache, so it should take longer chat.invoke("Tell me a joke")
1 2 3 4
CPU times: user 1 µs, sys: 0 ns, total: 1 µs Wall time: 4.77 µs
AIMessage(content="Sure, here's one for you:\n\nWhy did the tomato turn red?\n\nBecause it saw the salad dressing!", response_metadata={'token_usage': {'completion_tokens': 23, 'prompt_tokens': 7, 'total_tokens': 30}, 'model_name': 'moonshot-v1-8k', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None})
1 2 3
# %time # The second time it is, so it goes faster chat.invoke("Tell me a joke")
1 2 3 4
CPU times: user 1e+03 ns, sys: 0 ns, total: 1e+03 ns Wall time: 2.86 µs
AIMessage(content="Sure, here's one for you:\n\nWhy did the tomato turn red?\n\nBecause it saw the salad dressing!", response_metadata={'token_usage': {'completion_tokens': 23, 'prompt_tokens': 7, 'total_tokens': 30}, 'model_name': 'moonshot-v1-8k', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None})