Created
October 27, 2025 13:37
-
-
Save larkintuckerllc/746bf7f41e7137158a919e27ef3a8446 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from langchain.agents.middleware import dynamic_prompt | |
| from langchain_anthropic import ChatAnthropic | |
| from langchain.agents import create_agent | |
| from langchain_chroma import Chroma | |
| from langchain_openai import OpenAIEmbeddings | |
| def main(): | |
| embeddings = OpenAIEmbeddings( | |
| model="Qwen/Qwen3-Embedding-0.6B", | |
| openai_api_base="http://localhost:8000/v1", | |
| openai_api_key="" | |
| ) | |
| vector_store = Chroma( | |
| collection_name="example_collection", | |
| embedding_function=embeddings, | |
| persist_directory="./chroma_langchain_db", | |
| ) | |
| @dynamic_prompt | |
| def prompt_with_context(request): | |
| last_query = request.state["messages"][-1].text | |
| retrieved_docs = vector_store.similarity_search(last_query) | |
| docs_content = "\n\n".join(doc.page_content for doc in retrieved_docs) | |
| system_message = ( | |
| "You are a helpful assistant. Use the following context in your response:" | |
| f"\n\n{docs_content}" | |
| ) | |
| return system_message | |
| llm = ChatAnthropic( | |
| model="claude-sonnet-4-5", | |
| max_tokens=1000 | |
| ) | |
| agent = create_agent( | |
| model=llm, | |
| middleware=[prompt_with_context] | |
| ) | |
| result = agent.invoke( | |
| {"messages": [{"role": "user", "content": "How does water temperature affect clam growth?"}]} | |
| ) | |
| final_message = result["messages"][-1] | |
| print(final_message.content) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment