galo 3 сар өмнө
commit
c05efb5c7b
7 өөрчлөгдсөн 110 нэмэгдсэн , 0 устгасан
  1. 24 0
      Dockerfile
  2. 15 0
      README.md
  3. 16 0
      app/api/chat.py
  4. 12 0
      app/main.py
  5. 8 0
      app/schemas/chat.py
  6. 29 0
      app/services/qa.py
  7. 6 0
      requirements.txt

+ 24 - 0
Dockerfile

@@ -0,0 +1,24 @@
+# Use official Python slim image
+FROM python:3.10-slim
+
+# Set working directory
+WORKDIR /app
+
+# Install system dependencies for Ollama
+RUN apt-get update && apt-get install -y curl libgomp1 && rm -rf /var/lib/apt/lists/*
+
+# Install Ollama
+RUN curl -fsSL https://ollama.com/install.sh | sh
+
+# Copy requirements and install Python dependencies
+COPY requirements.txt .
+RUN pip install --no-cache-dir -r requirements.txt
+
+# Copy the application code
+COPY . .
+
+# Expose port 8000 for FastAPI
+EXPOSE 8000
+
+# Command to start Ollama server, pull the model if needed, and run FastAPI
+CMD ["sh", "-c", "ollama serve & (ollama list | grep -q mistral || ollama pull mistral) && uvicorn app.main:app --host 0.0.0.0 --port 8000 & wait"]

+ 15 - 0
README.md

@@ -0,0 +1,15 @@
+# Docker de chat usando LangChain e FastAPI
+
+ultiliza Ollama e Mistral, requer recursos para a LLM local
+
+## Prerequisites
+- Git
+- Docker
+
+## Setup and Installation
+
+1. git clone https://github.com/your-username/takehome-python.git
+2. cd LangChain
+3. docker build -t chat-api .
+4. docker run -p 8000:8000 --env-file .env chat-api
+5. curl -X POST http://localhost:8000/chat/ -H "Content-Type: application/json" -d "{\"message\": \"Qual a capital da França?\"}"

+ 16 - 0
app/api/chat.py

@@ -0,0 +1,16 @@
+from fastapi import APIRouter, HTTPException
+from app.schemas.chat import ChatRequest, ChatResponse
+from app.services.qa import get_answer
+import time
+
+router = APIRouter()
+
[email protected]("/", response_model=ChatResponse)
+async def chat(request: ChatRequest):
+    try:
+        start_time = time.time()
+        answer = get_answer(request.message)
+        latency_ms = int((time.time() - start_time) * 1000)
+        return ChatResponse(answer=answer, latency_ms=latency_ms)
+    except Exception as e:
+        raise HTTPException(status_code=500, detail=str(e))

+ 12 - 0
app/main.py

@@ -0,0 +1,12 @@
+from fastapi import FastAPI
+from app.api.chat import router as chat_router
+
+app = FastAPI(title="Chat API with LangChain")
+
+# Include the chat router
+app.include_router(chat_router, prefix="/chat")
+
+# Health check endpoint
[email protected]("/health")
+async def health_check():
+    return {"status": "healthy"}

+ 8 - 0
app/schemas/chat.py

@@ -0,0 +1,8 @@
+from pydantic import BaseModel
+
+class ChatRequest(BaseModel):
+    message: str
+
+class ChatResponse(BaseModel):
+    answer: str
+    latency_ms: int

+ 29 - 0
app/services/qa.py

@@ -0,0 +1,29 @@
+from langchain_community.llms import Ollama
+from langchain.chains import LLMChain
+from langchain.prompts import PromptTemplate
+import os
+from dotenv import load_dotenv
+
+# Load environment variables
+load_dotenv()
+
+# Initialize Ollama LLM
+llm = Ollama(
+    model="mistral",
+    temperature=0.7
+)
+
+# Define a simple prompt template
+prompt = PromptTemplate(
+    input_variables=["question"],
+    template="Responda à seguinte pergunta: {question}"
+)
+
+# Create the LLM chain
+chain = LLMChain(llm=llm, prompt=prompt)
+
+def get_answer(question: str) -> str:
+    return chain.run(question=question)
+
+if __name__ == "__main__":
+    print(get_answer("Qual a capital da França?"))

+ 6 - 0
requirements.txt

@@ -0,0 +1,6 @@
+fastapi==0.115.0
+uvicorn==0.30.6
+langchain==0.2.16
+langchain_community==0.2.16
+pydantic==2.9.2
+python-dotenv==1.0.1