注意:如果在设置客户端时更改了 model 或其他参数如 temperature 或 max_tokens,它将覆盖在 LaunchPad 中使用的现有默认配置。
Copy
import osimport getpassif "PREMAI_API_KEY" not in os.environ: os.environ["PREMAI_API_KEY"] = getpass.getpass("PremAI API Key:")chat = ChatPremAI(project_id=1234, model_name="gpt-4.1")
human_message = HumanMessage(content="Who are you?")response = chat.invoke([human_message])print(response.content)
您可以像这样在这里提供系统提示:
Copy
system_message = SystemMessage(content="You are a friendly assistant.")human_message = HumanMessage(content="Who are you?")chat.invoke([system_message, human_message])
Dense retrieval models typically include:1. **BERT-based Models**: Such as DPR (Dense Passage Retrieval) which uses BERT for encoding queries and passages.2. **ColBERT**: A model that combines BERT with late interaction mechanisms.3. **ANCE (Approximate Nearest Neighbor Negative Contrastive Estimation)**: Uses BERT and focuses on efficient retrieval.4. **TCT-ColBERT**: A variant of ColBERT that uses a two-tower{ "document_chunks": [ { "repository_id": 1985, "document_id": 1306, "chunk_id": 173899, "document_name": "[D] Difference between sparse and dense information\u2026", "similarity_score": 0.3209080100059509, "content": "with the difference or anywhere\nwhere I can read about it?\n\n\n 17 9\n\n\n u/ScotiabankCanada \u2022 Promoted\n\n\n Accelerate your study permit process\n with Scotiabank's Student GIC\n Program. We're here to help you tur\u2026\n\n\n startright.scotiabank.com Learn More\n\n\n Add a Comment\n\n\nSort by: Best\n\n\n DinosParkour \u2022 1y ago\n\n\n Dense Retrieval (DR) m" } ]}
import sysfor chunk in chat.stream("hello how are you"): sys.stdout.write(chunk.content) sys.stdout.flush()
与上面类似,如果您想覆盖系统提示和生成参数,您需要添加以下内容:
Copy
import sysfor chunk in chat.stream( "hello how are you", system_prompt = "You are an helpful assistant", temperature = 0.7, max_tokens = 20): sys.stdout.write(chunk.content) sys.stdout.flush()
这将逐个 Token 流式传输。
请注意:截至目前,不支持带有流式传输的 RAG。但我们仍然通过 API 支持它。您可以在 此处 了解更多信息。
model = "text-embedding-3-large"embedder = PremEmbeddings(project_id=8, model=model)query = "Hello, this is a test query"query_result = embedder.embed_query(query)# 打印查询嵌入向量的前五个元素print(query_result[:5])
与聊天不同,对于 PremAIEmbeddings 设置 model_name 参数是强制性的。
最后,让我们嵌入一些示例文档
Copy
documents = [ "This is document1", "This is document2", "This is document3"]doc_result = embedder.embed_documents(documents)# 与前面的结果类似,打印第一个文档向量的前五个元素print(doc_result[0][:5])
Copy
print(f"Dimension of embeddings: {len(query_result)}")
from langchain.tools import toolfrom pydantic import BaseModel, Field# 定义函数参数的模式class OperationInput(BaseModel): a: int = Field(description="First number") b: int = Field(description="Second number")# 现在定义函数,参数模式将是 OperationInput@tool("add", args_schema=OperationInput, return_direct=True)def add(a: int, b: int) -> int: """Adds `a` and `b`. Args: a: First int b: Second int """ return a + b@tool("multiply", args_schema=OperationInput, return_direct=True)def multiply(a: int, b: int) -> int: """Multiplies a and b. Args: a: First int b: Second int """ return a * b