流式输出怎么弄,谢谢 #4766

Closed
opened 2025-11-12 12:30:37 -06:00 by GiteaMirror · 1 comment
Owner

Originally created by @NXL333 on GitHub (Nov 4, 2024).

What is the issue?

初始化 Ollama 模型和嵌入

llm = ChatOllama(model="llama3.1")
embeddings = OllamaEmbeddings(model="nomic-embed-text")

准备文档

text = """
Datawhale 是一个专注于数据科学与 AI 领域的开源组织,汇集了众多领域院校和知名企业的优秀学习者,聚合了一群有开源精神和探索精神的团队成员。
Datawhale 以" for the learner,和学习者一起成长"为愿景,鼓励真实地展现自我、开放包容、互信互助、敢于试错和勇于担当。
同时 Datawhale 用开源的理念去探索开源内容、开源学习和开源方案,赋能人才培养,助力人才成长,建立起人与人,人与知识,人与企业和人与未来的联结。
如果你想在Datawhale开源社区发起一个开源项目,请详细阅读Datawhale开源项目指南[https://github.com/datawhalechina/DOPMC/blob/main/GUIDE.md]
"""

分割文本

text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=20)
chunks = text_splitter.split_text(text)

创建向量存储

vectorstore = FAISS.from_texts(chunks, embeddings)
retriever = vectorstore.as_retriever()

创建提示模板

template = """只能使用下列内容回答问题:
{context}

Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)

创建检索-问答链

chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
)

使用链回答问题

question = "我想为datawhale贡献该怎么做?"
response = chain.invoke(question)

这是你们的DEMO代码,我想弄成流式输出,可以吗???怎么弄,谢谢

OS

Windows

GPU

Nvidia

CPU

Intel

Ollama version

No response

Originally created by @NXL333 on GitHub (Nov 4, 2024). ### What is the issue? # 初始化 Ollama 模型和嵌入 llm = ChatOllama(model="llama3.1") embeddings = OllamaEmbeddings(model="nomic-embed-text") # 准备文档 text = """ Datawhale 是一个专注于数据科学与 AI 领域的开源组织,汇集了众多领域院校和知名企业的优秀学习者,聚合了一群有开源精神和探索精神的团队成员。 Datawhale 以" for the learner,和学习者一起成长"为愿景,鼓励真实地展现自我、开放包容、互信互助、敢于试错和勇于担当。 同时 Datawhale 用开源的理念去探索开源内容、开源学习和开源方案,赋能人才培养,助力人才成长,建立起人与人,人与知识,人与企业和人与未来的联结。 如果你想在Datawhale开源社区发起一个开源项目,请详细阅读Datawhale开源项目指南[https://github.com/datawhalechina/DOPMC/blob/main/GUIDE.md] """ # 分割文本 text_splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=20) chunks = text_splitter.split_text(text) # 创建向量存储 vectorstore = FAISS.from_texts(chunks, embeddings) retriever = vectorstore.as_retriever() # 创建提示模板 template = """只能使用下列内容回答问题: {context} Question: {question} """ prompt = ChatPromptTemplate.from_template(template) # 创建检索-问答链 chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | llm ) # 使用链回答问题 question = "我想为datawhale贡献该怎么做?" response = chain.invoke(question) 这是你们的DEMO代码,我想弄成流式输出,可以吗???怎么弄,谢谢 ### OS Windows ### GPU Nvidia ### CPU Intel ### Ollama version _No response_
GiteaMirror added the question label 2025-11-12 12:30:37 -06:00
Author
Owner

@rick-github commented on GitHub (Nov 4, 2024):

https://python.langchain.com/docs/how_to/streaming/#chains

@rick-github commented on GitHub (Nov 4, 2024): https://python.langchain.com/docs/how_to/streaming/#chains
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama-ollama#4766