Преглед изворни кода

able to run taide locally in ollama, not docker

SherryLiu пре 7 месеци
родитељ
комит
ec3fce5b51
4 измењених фајлова са 100 додато и 63 уклоњено
  1. 0 35
      TAIDE-test.py
  2. 25 27
      docker-compose.yml
  3. 74 0
      ollama_chat.py
  4. 1 1
      run.sh

+ 0 - 35
TAIDE-test.py

@@ -1,35 +0,0 @@
-import requests
-import json 
-
-OLLAMA_API_URL = "http://localhost:11434/api/chat"
-
-def query_ollama_taide(messages):
-    payload = {
-        "model": "taide-local",
-        "prompt": messages,
-        "stream": False
-    }
-
-    try:
-        response = requests.post(OLLAMA_API_URL, json=payload)
-        response.raise_for_status()
-        result = response.json()
-        return result.get('message', {}).get('content', '')
-    except requests.RequestException as e:
-        print(f"Error calling Ollama API:{e}")
-        return None 
-    
-def simple_rag(question, context):
-    system_prompt = "你是一個來自台灣的AI助理,你的名字是 TAIDE,樂於以台灣人的立場幫助使用者,會用繁體中文回答問題。"
-    user_prompt = f"根據以下上下文回答問題:\n\n上下文:{context}\n\n問題:{question}\n\n請提供簡潔的回答。"
-
-    messages = [
-        {"role": "system", "content":system_prompt},
-        {"role":"user", "content":user_prompt}
-    ]
-    
-    return query_ollama_taide(messages)
-
-if __name__ == "__main__":
-    context = "台灣在2050年訂立了淨零排放目標,並制定了相關法規和政策來推動減碳。"
-    question = "台灣的溫室氣體排放法規目標是什麼?"

+ 25 - 27
docker-compose.yml

@@ -1,4 +1,25 @@
-# version: '3'
+services:
+  # ollama:
+  #   image: ollama/ollama
+  #   volumes:
+  #     - ollama:/root/.ollama
+  #     - /Users/sherry/Documents/_Personal/ChoozeMo/notebooks/carbon/llm/ollama:/models
+  #   ports:
+  #     - "11434:11434"
+  #   mem_limit: 16g
+  #   cpus: 6
+
+  redis: 
+    image: redis/redis-stack:latest
+    ports:
+      - "6379:6379"
+      - "8001:8001"
+
+volumes:
+  ollama:
+
+
+
 # services:
 #   ollama:
 #     image: ollama/ollama
@@ -9,35 +30,12 @@
 #       - "11434:11434"
 #     mem_limit: 16g
 #     cpus: 6
-#     command: sh -c "ollama create taide-local -f /models/ide-7b-a.2-q4_k_m.gguf && ollama run taide-local"
 
-#   redis: 
-#     image: redis:redit-stack:latest
+#   redis:
+#     image: redis/redis-stack:latest
 #     ports:
 #       - "6379:6379"
 #       - "8001:8001"
 
 # volumes:
-#   ollama:
-
-
-
-services:
-  ollama:
-    image: ollama/ollama
-    volumes:
-      - ollama:/root/.ollama
-      - /Users/sherry/Documents/_Personal/ChoozeMo/notebooks/carbon/llm/ollama:/models
-    ports:
-      - "11434:11434"
-    mem_limit: 16g
-    cpus: 6
-
-  redis:
-    image: redis/redis-stack:latest
-    ports:
-      - "6379:6379"
-      - "8001:8001"
-
-volumes:
-  ollama:
+#   ollama:

+ 74 - 0
ollama_chat.py

@@ -0,0 +1,74 @@
+import subprocess
+import json
+from typing import Any, List, Optional, Dict
+from langchain_core.callbacks import CallbackManagerForLLMRun
+from langchain_core.language_models import BaseChatModel
+from langchain_core.messages import BaseMessage, AIMessage, HumanMessage
+from langchain_core.outputs import ChatResult, ChatGeneration
+from pydantic import Field
+
+class OllamaChatModel(BaseChatModel):
+    model_name: str = Field(default="taide-local")
+
+    def _generate(
+        self,
+        messages: List[BaseMessage],
+        stop: Optional[List[str]] = None,
+        run_manager: Optional[CallbackManagerForLLMRun] = None,
+        **kwargs: Any,
+    ) -> ChatResult:
+        prompt = "\n".join([f"{msg.__class__.__name__}: {msg.content}" for msg in messages])
+        
+        command = ["ollama", "run", self.model_name, prompt]
+        result = subprocess.run(command, capture_output=True, text=True)
+        
+        if result.returncode != 0:
+            raise Exception(f"Ollama command failed: {result.stderr}")
+        
+        content = result.stdout.strip()
+        
+        message = AIMessage(content=content)
+        generation = ChatGeneration(message=message)
+        return ChatResult(generations=[generation])
+
+    @property
+    def _llm_type(self) -> str:
+        return "ollama-chat-model"
+
+def check_model_availability(model_name: str):
+    result = subprocess.run(["ollama", "list"], capture_output=True, text=True)
+    if result.returncode != 0:
+        print(f"Error checking model availability: {result.stderr}")
+        return False
+    
+    models = result.stdout.splitlines()
+    return any(model_name in model for model in models)
+
+# Usage example
+if __name__ == "__main__":
+    model_name = "taide-local"
+
+    print(f"Checking availability of model {model_name}...")
+    if not check_model_availability(model_name):
+        print(f"Model {model_name} is not available. Please check if it's correctly installed in Ollama.")
+        exit(1)
+
+    model = OllamaChatModel(model_name=model_name)
+    
+    print(f"Starting chat with {model_name} model. Type 'exit' to quit.")
+    
+    messages = []
+    while True:
+        user_input = input("You: ")
+        if user_input.lower() == 'exit':
+            break
+        
+        messages.append(HumanMessage(content=user_input))
+        try:
+            response = model.invoke(messages)
+            print("AI:", response.content)
+            messages.append(AIMessage(content=response.content))
+        except Exception as e:
+            print(f"Error communicating with Ollama: {e}")
+
+print("Chat session ended. Goodbye!")

+ 1 - 1
run.sh

@@ -31,7 +31,7 @@ sleep 20
 # Change to the directory containing Python script
 cd "$script_dir/systex-RAG-sherry"
 echo "Running RAG application..."
-python RAG_app_copy.py
+python ollama_chat.py
 
 # 使脚本文件可执行:
 # chmod +x run.sh