File size: 3,686 Bytes
0fa4cc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
"""
Minimalna aplikacja LLM w 50 linijkach!
Streamlit + Ollama = zero konfiguracji
"""

import streamlit as st
import ollama
import os
from typing import Generator

# Konfiguracja
OLLAMA_URL = os.getenv("OLLAMA_URL", "http://localhost:11434")
MODEL_NAME = "mistral:7b-instruct"

# Setup Ollama client
client = ollama.Client(host=OLLAMA_URL)

def stream_response(prompt: str) -> Generator[str, None, None]:
    """Generator dla streaming response"""
    try:
        stream = client.chat(
            model=MODEL_NAME,
            messages=[{"role": "user", "content": prompt}],
            stream=True
        )
        
        for chunk in stream:
            if chunk['message']['content']:
                yield chunk['message']['content']
    except Exception as e:
        yield f"Error: {str(e)}"

def main():
    # UI Setup
    st.set_page_config(
        page_title="πŸ€– Minimal LLM Chat",
        page_icon="πŸ€–",
        layout="wide"
    )
    
    st.title("πŸ€– Minimal LLM Chat")
    st.markdown("*Powered by Ollama + Mistral 7B*")
    
    # Sidebar z ustawieniami
    with st.sidebar:
        st.header("βš™οΈ Settings")
        
        # Model info
        try:
            models = client.list()
            available_models = [m['name'] for m in models['models']]
            st.success(f"βœ… Connected to Ollama")
            st.info(f"Available models: {len(available_models)}")
        except:
            st.error("❌ Cannot connect to Ollama")
            st.stop()
        
        # Parameters
        temperature = st.slider("Temperature", 0.0, 2.0, 0.7, 0.1)
        max_tokens = st.slider("Max tokens", 50, 1000, 500, 50)
        
        # System prompt
        system_prompt = st.text_area(
            "System prompt:", 
            "You are a helpful AI assistant. Answer concisely and accurately.",
            height=100
        )
    
    # Chat interface
    if "messages" not in st.session_state:
        st.session_state.messages = []
    
    # Display chat history
    for message in st.session_state.messages:
        with st.chat_message(message["role"]):
            st.markdown(message["content"])
    
    # Chat input
    if prompt := st.chat_input("Type your message here..."):
        # Add user message
        st.session_state.messages.append({"role": "user", "content": prompt})
        with st.chat_message("user"):
            st.markdown(prompt)
        
        # Generate response
        with st.chat_message("assistant"):
            response_placeholder = st.empty()
            full_response = ""
            
            # Streaming response
            for chunk in stream_response(prompt):
                full_response += chunk
                response_placeholder.markdown(full_response + "β–Œ")
            
            response_placeholder.markdown(full_response)
        
        # Add assistant message
        st.session_state.messages.append({"role": "assistant", "content": full_response})
    
    # Quick actions
    col1, col2, col3 = st.columns(3)
    
    with col1:
        if st.button("πŸ—‘οΈ Clear Chat"):
            st.session_state.messages = []
            st.rerun()
    
    with col2:
        if st.button("πŸ’‘ Example Question"):
            example = "Explain quantum computing in simple terms"
            st.session_state.messages.append({"role": "user", "content": example})
            st.rerun()
    
    with col3:
        if st.button("πŸ“Š Model Info"):
            try:
                info = client.show(MODEL_NAME)
                st.json(info)
            except:
                st.error("Cannot get model info")

if __name__ == "__main__":
    main()