Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/691c325a-b61c-8003-b613-87e7ee20e0bf
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== gpt-5-1 == === User: ทำ ข้อ 3 แต่อย่างจะ ใช้ LLM ของ openAi และ deepseek แทน === ทำ ข้อ 3 แต่อย่างจะ ใช้ LLM ของ openAi และ deepseek แทน === Assistant: ได้ครับ — ผมจะทำ ข้อ 3: Tiny Demo แบบครบระบบ === ได้ครับ — ผมจะทำ ข้อ 3: Tiny Demo แบบครบระบบ โดยปรับให้ใช้ LLM ของ OpenAI หรือ DeepSeek (เลือกได้) ในบทบาท: * ใช้ OpenAI หรือ DeepSeek → สำหรับ LLM (สรุป, extract insight, generate responses) * ใช้ SentenceTransformer → สำหรับ embedding (เพราะราคาถูก/เร็ว และไม่ผูก vendor) * ใช้ Weaviate → vector memory store * มี Memory API (minimal) → read / write memory * มี Agent Loop (LLM) → ใช้ memory ก่อนตอบ และเขียน memory หลังตอบ ทั้งหมดนี้เป็น โค้ดที่รันได้จริง บนเครื่องคุณ (Python) เหมาะสำหรับทดลองว่า “memory layer ทำงานอย่างไร” ก่อน scale. === ✅ TINY DEMO — “Agentic Memory Layer” (OpenAI/DeepSeek + Weaviate) === ==== 0) เตรียมของก่อนเริ่ม ==== ===== ติดตั้ง dependencies ===== <syntaxhighlight lang="bash">pip install weaviate-client sentence-transformers python-dotenv openai </syntaxhighlight> ===== สร้างไฟล์ .env ===== <syntaxhighlight lang="env">OPENAI_API_KEY=your-key-here DEEPSEEK_API_KEY=your-key-here # ถ้าใช้ DeepSeek USE_MODEL=openai # เลือก: openai / deepseek </syntaxhighlight> === 1) สร้าง Weaviate (Local) === ใช้ Docker: <syntaxhighlight lang="yaml"># docker-compose.yml version: '3.4' services: weaviate: image: semitechnologies/weaviate:1.24.10 ports: * "8080:8080" environment: QUERY_DEFAULTS_LIMIT: 25 AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED: 'true' PERSISTENCE_DATA_PATH: './data' CLUSTER_HOSTNAME: 'node1' </syntaxhighlight> รัน: <syntaxhighlight lang="bash">docker compose up -d </syntaxhighlight> === 2) โค้ด Demo (ครบระบบ) === สร้างไฟล์: memory_demo.py <syntaxhighlight lang="python">import os from datetime import datetime from dotenv import load_dotenv import weaviate from sentence_transformers import SentenceTransformer from openai import OpenAI load_dotenv() === --------------------------- === === CONFIG === === --------------------------- === USE_MODEL = os.getenv("USE_MODEL", "openai") # openai | deepseek OPENAI_KEY = os.getenv("OPENAI_API_KEY") DEEPSEEK_KEY = os.getenv("DEEPSEEK_API_KEY") client_llm = OpenAI(api_key=OPENAI_KEY) if USE_MODEL == "openai" else OpenAI( api_key=DEEPSEEK_KEY, base_url="https://api.deepseek.com" ) === --------------------------- === === 1) WEAVIATE CLIENT + SCHEMA === === --------------------------- === wclient = weaviate.Client("http://localhost:8080") if not wclient.schema.exists("Memory"): wclient.schema.create_class({ "class": "Memory", "vectorizer": "none", "properties": [ {"name": "text", "dataType": ["text"]}, {"name": "summary", "dataType": ["text"]}, {"name": "user_id", "dataType": ["text"]}, {"name": "importance", "dataType": ["number"]}, {"name": "timestamp", "dataType": ["text"]}, ] }) === --------------------------- === === 2) Embedding Model === === --------------------------- === encoder = SentenceTransformer("all-MiniLM-L6-v2") === --------------------------- === === 3) MEMORY WRITE === === --------------------------- === def write_memory(user_id, text, importance=1): embedding = encoder.encode(text).tolist() obj = { "text": text, "summary": None, "user_id": user_id, "importance": importance, "timestamp": datetime.utcnow().isoformat() } wclient.data_object.create(obj, "Memory", vector=embedding) === --------------------------- === === 4) MEMORY QUERY === === --------------------------- === def read_memory(query, top_k=5): q_vec = encoder.encode(query).tolist() res = wclient.query.get("Memory", ["text", "timestamp", "importance", "_additional {distance}"]) \ .with_near_vector({"vector": q_vec}) \ .with_limit(top_k) \ .do() return res["data"]["Get"]["Memory"] === --------------------------- === === 5) LLM CALL (OpenAI / DeepSeek) === === --------------------------- === def call_llm(prompt): model_name = "gpt-4.1-mini" if USE_MODEL == "openai" else "deepseek-chat" completion = client_llm.chat.completions.create( model=model_name, messages=[{"role": "user", "content": prompt}] ) return completion.choices[0].message["content"] === --------------------------- === === 6) AGENT LOOP === === --------------------------- === def agent(user_id, user_msg): # (A) ดึง memory ที่เกี่ยวข้อง memories = read_memory(user_msg) memory_text = "\n".join([f"- {m['text']}" for m in memories]) # (B) สร้าง prompt ให้ model ใช้ข้อมูลจาก memory prompt = f""" You are an AI assistant with long-term memory. User message: {user_msg} Relevant memories: {memory_text if memory_text else "(none)"} Use the memories if they are relevant. Produce a concise and helpful answer. """ reply = call_llm(prompt) # (C) สร้าง insight เพื่อนำไปเก็บใน memory insight_prompt = f""" Extract ONE useful long-term memory from the conversation below. Make it short, factual, and helpful for future interactions. If nothing useful, return "NO_MEMORY". Conversation: User: {user_msg} Assistant: {reply} """ insight = call_llm(insight_prompt).strip() if insight != "NO_MEMORY": write_memory(user_id, insight) return reply, insight === --------------------------- === === DEMO RUN === === --------------------------- === if __name__ == "__main__": user_id = "demo-user" while True: msg = input("User: ") answer, mem = agent(user_id, msg) print("\nAssistant:", answer) print("Saved memory:", mem) print("-" * 50) </syntaxhighlight> === 3) วิธีทดสอบ (Step-by-step) === ===== 1) เริ่ม Weaviate ===== <syntaxhighlight lang="bash">docker compose up -d </syntaxhighlight> ===== 2) รัน demo ===== <syntaxhighlight lang="bash">python memory_demo.py </syntaxhighlight> ===== 3) พิมพ์ข้อความสัก 2–3 รอบ ===== ตัวอย่าง: <syntaxhighlight>User: ผมชอบกาแฟแบบไม่หวานนะ Assistant: ... Saved memory: User prefers coffee without sugar. </syntaxhighlight> จากนั้น: <syntaxhighlight>User: แนะนำเมนูกาแฟให้หน่อย Assistant: (ควรจำได้ว่า user ไม่ชอบหวาน) </syntaxhighlight> === 4) คุณสามารถสลับใช้ OpenAI หรือ DeepSeek ได้ทันที === ใน .env เปลี่ยน: <syntaxhighlight>USE_MODEL=openai </syntaxhighlight> หรือ <syntaxhighlight>USE_MODEL=deepseek </syntaxhighlight> ระบบ memory ไม่เปลี่ยน เพราะมันอยู่ที่ Weaviate + embedding model === 5) หากต้องการเวอร์ชัน “Agent + Tools + Retrieval + Summarizer” === ผมสามารถ: * ทำเป็น FastAPI Memory Service * ทำเป็น MCP server ต่อกับ Claude Desktop * สร้าง diagram (architecture / sequence) * ทำเป็น Docker Compose ครบชุด บอกผมได้เลยว่าต้องการระดับ “production-ish” แค่ไหนครับ!
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)