File size: 3,188 Bytes
deb019b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

LLM Chat Application
====================

This repository contains a FastAPI application for chatting and question answering using a Large Language Model (LLM). The application supports conversational interactions with memory as well as simple Q&A without conversation history.

Features
--------

*   **Chat with history**: Interact with the model while maintaining a conversation history.
*   **Simple Q&A**: Ask questions and get answers without maintaining a conversation history.
*   **Streaming Responses**: Receive responses in a streaming fashion.

Routes
------

### `/chat`

*   **Method**: `POST`
*   **Description**: Chat with the model while maintaining conversation history.
*   **Request Body**:
    *   `input` (string): The input message from the user.
*   **Response**: Streaming response of the model's reply.

### `/ask`

*   **Method**: `POST`
*   **Description**: Ask a question and get a response without maintaining conversation history.
*   **Request Body**:
    *   `input` (string): The input question from the user.
*   **Response**: Streaming response of the model's answer.

Getting Started
---------------

### Prerequisites

*   Python 3.7+
*   Docker (optional, for containerized deployment)
*   A valid `GROQ_API_KEY` (from Groq API)

### Installation

1.  **Clone the repository**:
    
        git clone https://github.com/your-username/llm-chat-app.git
        cd llm-chat-app
    
2.  **Create a virtual environment and activate it**:
    
        python -m venv venv
        source venv/bin/activate  # On Windows use `venv\Scripts\activate`
    
3.  **Install the dependencies**:
    
        pip install -r requirements.txt
    
4.  **Create a `.env` file and add your `GROQ_API_KEY`**:
    
        echo "GROQ_API_KEY=your_groq_api_key" > .env
    

### Running the Application

1.  **Start the FastAPI server**:
    
        uvicorn main:app --reload
    
2.  **The application will be available at** `http://127.0.0.1:8000`.

### Using the Application

You can interact with the application using tools like `curl`, Postman, or directly from your frontend application.

#### Example Requests

**Chat with History**:

    curl -X POST "http://127.0.0.1:8000/chat" -H "Content-Type: application/json" -d '{"input": "Hello, how are you?"}'

**Simple Q&A**:

    curl -X POST "http://127.0.0.1:8000/ask" -H "Content-Type: application/json" -d '{"input": "What is the capital of France?"}'

### Testing

For testing purposes, you can use the base URL `https://llamachat-ipea.onrender.com/`.

**Chat with History**:

    curl -X POST "https://llamachat-ipea.onrender.com/chat" -H "Content-Type: application/json" -d '{"input": "Hello, how are you?"}'

**Simple Q&A**:

    curl -X POST "https://llamachat-ipea.onrender.com/ask" -H "Content-Type: application/json" -d '{"input": "What is the capital of France?"}'

### Docker Deployment (Optional)

To run the application in a Docker container:

1.  **Build the Docker image**:
    
        docker build -t llm-chat-app .
    
2.  **Run the Docker container**:
    
        docker run -d -p 8000:8000 --env-file .env llm-chat-app
    

The application will be available at `http://127.0.0.1:8000`.