henryz lukestanley commited on
Commit
bd5197f
0 Parent(s):

Duplicate from lukestanley/streaming_chat_with_gpt-3.5-turbo_using_langchain_sorta

Browse files
Files changed (4) hide show
  1. .gitattributes +34 -0
  2. README.md +14 -0
  3. app.py +83 -0
  4. requirements.txt +2 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Streaming Chat With Gpt-3.5-turbo Using Langchain Sorta
3
+ emoji: 📚
4
+ colorFrom: purple
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 3.19.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ duplicated_from: lukestanley/streaming_chat_with_gpt-3.5-turbo_using_langchain_sorta
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List, Tuple, Dict, Generator
2
+ from langchain.llms import OpenAI
3
+ import gradio as gr
4
+
5
+ model_name = "gpt-3.5-turbo"
6
+ LLM = OpenAI(model_name=model_name, temperature=0.1)
7
+
8
+ def create_history_messages(history: List[Tuple[str, str]]) -> List[dict]:
9
+ history_messages = [{"role": "user", "content": m[0]} for m in history]
10
+ history_messages.extend([{"role": "assistant", "content": m[1]} for m in history])
11
+ return history_messages
12
+
13
+ def create_formatted_history(history_messages: List[dict]) -> List[Tuple[str, str]]:
14
+ formatted_history = []
15
+ user_messages = []
16
+ assistant_messages = []
17
+
18
+ for message in history_messages:
19
+ if message["role"] == "user":
20
+ user_messages.append(message["content"])
21
+ elif message["role"] == "assistant":
22
+ assistant_messages.append(message["content"])
23
+
24
+ if user_messages and assistant_messages:
25
+ formatted_history.append(
26
+ ("".join(user_messages), "".join(assistant_messages))
27
+ )
28
+ user_messages = []
29
+ assistant_messages = []
30
+
31
+ # append any remaining messages
32
+ if user_messages:
33
+ formatted_history.append(("".join(user_messages), None))
34
+ elif assistant_messages:
35
+ formatted_history.append((None, "".join(assistant_messages)))
36
+
37
+ return formatted_history
38
+
39
+ def chat(
40
+ message: str, state: List[Dict[str, str]], client = LLM.client
41
+ ) -> Generator[Tuple[List[Tuple[str, str]], List[Dict[str, str]]], None, None]:
42
+ history_messages = state
43
+ if history_messages == None:
44
+ history_messages = []
45
+ history_messages.append({"role": "system", "content": "A helpful assistant."})
46
+
47
+ history_messages.append({"role": "user", "content": message})
48
+ # We have no content for the assistant's response yet but we will update this:
49
+ history_messages.append({"role": "assistant", "content": ""})
50
+
51
+ response_message = ""
52
+
53
+ chat_generator = client.create(
54
+ messages=history_messages, stream=True, model=model_name
55
+ )
56
+
57
+ for chunk in chat_generator:
58
+ if "choices" in chunk:
59
+ for choice in chunk["choices"]:
60
+ if "delta" in choice and "content" in choice["delta"]:
61
+ new_token = choice["delta"]["content"]
62
+ # Add the latest token:
63
+ response_message += new_token
64
+ # Update the assistant's response in our model:
65
+ history_messages[-1]["content"] = response_message
66
+
67
+ if "finish_reason" in choice and choice["finish_reason"] == "stop":
68
+ break
69
+ formatted_history = create_formatted_history(history_messages)
70
+ yield formatted_history, history_messages
71
+
72
+ chatbot = gr.Chatbot(label="Chat").style(color_map=("yellow", "purple"))
73
+ iface = gr.Interface(
74
+ fn=chat,
75
+ inputs=[
76
+ gr.Textbox(placeholder="Hello! How are you? etc.", label="Message"),
77
+ "state",
78
+ ],
79
+ outputs=[chatbot, "state"],
80
+ allow_flagging="never",
81
+ )
82
+
83
+ iface.queue().launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ langchain
2
+ openai