|
7 | 7 | import time
|
8 | 8 |
|
9 | 9 | import streamlit as st
|
10 |
| -from api.api import CompletionRequest, OpenAiApiGenerator |
11 |
| - |
12 |
| -from build.builder import BuilderArgs, TokenizerArgs |
13 |
| - |
14 |
| -from generate import GeneratorArgs |
15 |
| - |
16 |
| - |
17 |
| -def main(args): |
18 |
| - builder_args = BuilderArgs.from_args(args) |
19 |
| - speculative_builder_args = BuilderArgs.from_speculative_args(args) |
20 |
| - tokenizer_args = TokenizerArgs.from_args(args) |
21 |
| - generator_args = GeneratorArgs.from_args(args) |
22 |
| - generator_args.chat_mode = False |
23 |
| - |
24 |
| - @st.cache_resource |
25 |
| - def initialize_generator() -> OpenAiApiGenerator: |
26 |
| - return OpenAiApiGenerator( |
27 |
| - builder_args, |
28 |
| - speculative_builder_args, |
29 |
| - tokenizer_args, |
30 |
| - generator_args, |
31 |
| - args.profile, |
32 |
| - args.quantize, |
33 |
| - args.draft_quantize, |
34 |
| - ) |
35 |
| - |
36 |
| - gen = initialize_generator() |
37 |
| - |
38 |
| - st.title("torchchat") |
39 |
| - |
40 |
| - # Initialize chat history |
41 |
| - if "messages" not in st.session_state: |
42 |
| - st.session_state.messages = [] |
43 |
| - |
44 |
| - # Display chat messages from history on app rerun |
45 |
| - for message in st.session_state.messages: |
46 |
| - with st.chat_message(message["role"]): |
47 |
| - st.markdown(message["content"]) |
48 |
| - |
49 |
| - # Accept user input |
50 |
| - if prompt := st.chat_input("What is up?"): |
51 |
| - # Add user message to chat history |
52 |
| - st.session_state.messages.append({"role": "user", "content": prompt}) |
53 |
| - # Display user message in chat message container |
54 |
| - with st.chat_message("user"): |
55 |
| - st.markdown(prompt) |
56 |
| - |
57 |
| - # Display assistant response in chat message container |
58 |
| - with st.chat_message("assistant"), st.status( |
59 |
| - "Generating... ", expanded=True |
60 |
| - ) as status: |
61 |
| - |
62 |
| - req = CompletionRequest( |
63 |
| - model=gen.builder_args.checkpoint_path, |
64 |
| - prompt=prompt, |
65 |
| - temperature=generator_args.temperature, |
66 |
| - messages=[], |
| 10 | +from openai import OpenAI |
| 11 | + |
| 12 | +st.title("torchchat") |
| 13 | + |
| 14 | +start_state = [ |
| 15 | + { |
| 16 | + "role": "system", |
| 17 | + "content": "You're an assistant. Answer questions directly, be brief, and have fun.", |
| 18 | + }, |
| 19 | + {"role": "assistant", "content": "How can I help you?"}, |
| 20 | +] |
| 21 | + |
| 22 | +with st.sidebar: |
| 23 | + response_max_tokens = st.slider( |
| 24 | + "Max Response Tokens", min_value=10, max_value=1000, value=250, step=10 |
| 25 | + ) |
| 26 | + if st.button("Reset Chat", type="primary"): |
| 27 | + st.session_state["messages"] = start_state |
| 28 | + |
| 29 | +if "messages" not in st.session_state: |
| 30 | + st.session_state["messages"] = start_state |
| 31 | + |
| 32 | + |
| 33 | +for msg in st.session_state.messages: |
| 34 | + st.chat_message(msg["role"]).write(msg["content"]) |
| 35 | + |
| 36 | +if prompt := st.chat_input(): |
| 37 | + client = OpenAI( |
| 38 | + base_url="http://127.0.0.1:5000/v1", |
| 39 | + api_key="813", # The OpenAI API requires an API key, but since we don't consume it, this can be any non-empty string. |
| 40 | + ) |
| 41 | + |
| 42 | + st.session_state.messages.append({"role": "user", "content": prompt}) |
| 43 | + st.chat_message("user").write(prompt) |
| 44 | + |
| 45 | + with st.chat_message("assistant"), st.status( |
| 46 | + "Generating... ", expanded=True |
| 47 | + ) as status: |
| 48 | + |
| 49 | + def get_streamed_completion(completion_generator): |
| 50 | + start = time.time() |
| 51 | + tokcount = 0 |
| 52 | + for chunk in completion_generator: |
| 53 | + tokcount += 1 |
| 54 | + yield chunk.choices[0].delta.content |
| 55 | + |
| 56 | + status.update( |
| 57 | + label="Done, averaged {:.2f} tokens/second".format( |
| 58 | + tokcount / (time.time() - start) |
| 59 | + ), |
| 60 | + state="complete", |
67 | 61 | )
|
68 | 62 |
|
69 |
| - def unwrap(completion_generator): |
70 |
| - start = time.time() |
71 |
| - tokcount = 0 |
72 |
| - for chunk_response in completion_generator: |
73 |
| - content = chunk_response.choices[0].delta.content |
74 |
| - if not gen.is_llama3_model or content not in set( |
75 |
| - gen.tokenizer.special_tokens.keys() |
76 |
| - ): |
77 |
| - yield content |
78 |
| - if content == gen.tokenizer.eos_id(): |
79 |
| - yield "." |
80 |
| - tokcount += 1 |
81 |
| - status.update( |
82 |
| - label="Done, averaged {:.2f} tokens/second".format( |
83 |
| - tokcount / (time.time() - start) |
84 |
| - ), |
85 |
| - state="complete", |
| 63 | + response = st.write_stream( |
| 64 | + get_streamed_completion( |
| 65 | + client.chat.completions.create( |
| 66 | + model="llama3", |
| 67 | + messages=st.session_state.messages, |
| 68 | + max_tokens=response_max_tokens, |
| 69 | + stream=True, |
86 | 70 | )
|
| 71 | + ) |
| 72 | + )[0] |
87 | 73 |
|
88 |
| - response = st.write_stream(unwrap(gen.completion(req))) |
89 |
| - |
90 |
| - # Add assistant response to chat history |
91 |
| - st.session_state.messages.append({"role": "assistant", "content": response}) |
| 74 | + st.session_state.messages.append({"role": "assistant", "content": response}) |
0 commit comments