Spaces:
Running
Running
Refactor chat functionality and add system prompt for DevalBot
Browse files- .DS_Store +0 -0
- .gradio/certificate.pem +31 -0
- __pycache__/bedrock_client.cpython-310.pyc +0 -0
- __pycache__/utils.cpython-310.pyc +0 -0
- app copy.py +89 -0
- bedrock_test.py +86 -79
- test copy.py +40 -0
.DS_Store
CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
|
|
.gradio/certificate.pem
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
-----BEGIN CERTIFICATE-----
|
2 |
+
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
|
31 |
+
-----END CERTIFICATE-----
|
__pycache__/bedrock_client.cpython-310.pyc
CHANGED
Binary files a/__pycache__/bedrock_client.cpython-310.pyc and b/__pycache__/bedrock_client.cpython-310.pyc differ
|
|
__pycache__/utils.cpython-310.pyc
ADDED
Binary file (669 Bytes). View file
|
|
app copy.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from bedrock_client import claude_llm, get_anthropic_client, claude_stream_response
|
3 |
+
from utils import load_users
|
4 |
+
|
5 |
+
AUTHS = load_users('user.csv')
|
6 |
+
HISTORY_LIMIT = 30 # max number of turns (user+assistant) to keep
|
7 |
+
|
8 |
+
|
9 |
+
# 1) Your system prompt
|
10 |
+
SYSTEM_PROMPT = (
|
11 |
+
"Du bist DevalBot, ein konversationeller Assistent des Deutschen Evaluierungsinstituts für Entwicklungsbewertung (DEval). DEval bietet staatlichen und zivilgesellschaftlichen Organisationen in der Entwicklungszusammenarbeit unabhängige und wissenschaftlich fundierte Evaluierungen. Deine Hauptsprache ist Deutsch; antworte daher standardmäßig auf Deutsch. Du kannst zudem bei statistischen Analysen und Programmierung in Stata und R unterstützen. Antworte sachlich, präzise und stelle bei Unklarheiten klärende Rückfragen."
|
12 |
+
)
|
13 |
+
|
14 |
+
def chat(user_message, history):
|
15 |
+
# ── 1) Guard against empty input ─────────────────────
|
16 |
+
if not user_message or not user_message.strip():
|
17 |
+
return
|
18 |
+
|
19 |
+
# ── 2) Build the LLM’s messages list ─────────────────
|
20 |
+
# Always start with the SYSTEM_PROMPT, then the UI history,
|
21 |
+
# then the new user turn:
|
22 |
+
llm_messages = [{"role":"system","content":SYSTEM_PROMPT}]
|
23 |
+
llm_messages += history
|
24 |
+
llm_messages.append({"role":"user","content":user_message})
|
25 |
+
|
26 |
+
# ── 3) Kick off the streaming call ───────────────────
|
27 |
+
client = get_anthropic_client()
|
28 |
+
streamer = lambda msgs: claude_stream_response(msgs, client)
|
29 |
+
|
30 |
+
# ── 4) Immediately show the user’s turn in the UI ─
|
31 |
+
ui_history = history + [{"role":"user","content":user_message}]
|
32 |
+
|
33 |
+
full_resp = ""
|
34 |
+
try:
|
35 |
+
for chunk in streamer(llm_messages):
|
36 |
+
full_resp += chunk
|
37 |
+
# yield the UI history plus the growing assistant bubble
|
38 |
+
yield ui_history + [{"role":"assistant","content": full_resp}]
|
39 |
+
except Exception as e:
|
40 |
+
# surface any error inline
|
41 |
+
err = f"⚠️ Oops, something went wrong: {e}"
|
42 |
+
yield ui_history + [{"role":"assistant","content": err}]
|
43 |
+
return
|
44 |
+
|
45 |
+
# ── 5) Finalize the assistant turn in the UI ─────────
|
46 |
+
ui_history.append({"role":"assistant","content": full_resp})
|
47 |
+
|
48 |
+
# ── 6) Trim to the last N turns ──────────────────────
|
49 |
+
if len(ui_history) > HISTORY_LIMIT:
|
50 |
+
ui_history = ui_history[-HISTORY_LIMIT:]
|
51 |
+
|
52 |
+
yield ui_history
|
53 |
+
|
54 |
+
|
55 |
+
with gr.Blocks(css_paths=["static/deval.css"],theme = gr.themes.Default(primary_hue="blue", secondary_hue="yellow"),) as demo:
|
56 |
+
# ── Logo + Header + Logout ────────────────────────────────
|
57 |
+
|
58 |
+
gr.Image(
|
59 |
+
value="static/logo.png",
|
60 |
+
show_label=False,
|
61 |
+
interactive=False,
|
62 |
+
show_download_button=False,
|
63 |
+
show_fullscreen_button=False,
|
64 |
+
elem_id="logo-primary", # matches the CSS above
|
65 |
+
)
|
66 |
+
|
67 |
+
#logout_btn = gr.Button("Logout", elem_id="logout-btn")
|
68 |
+
# inject auto-reload script
|
69 |
+
gr.HTML(
|
70 |
+
"""
|
71 |
+
<script>
|
72 |
+
// Reload the page after 1 minutes (300 000 ms)
|
73 |
+
setTimeout(() => {
|
74 |
+
window.location.reload();
|
75 |
+
}, 1000);
|
76 |
+
</script>
|
77 |
+
"""
|
78 |
+
)
|
79 |
+
gr.ChatInterface(
|
80 |
+
chat,
|
81 |
+
type="messages",
|
82 |
+
editable=True,
|
83 |
+
concurrency_limit=200,
|
84 |
+
save_history=True,
|
85 |
+
)
|
86 |
+
|
87 |
+
|
88 |
+
|
89 |
+
demo.queue().launch(auth=AUTHS, ssr_mode=False)
|
bedrock_test.py
CHANGED
@@ -7,7 +7,7 @@ import json
|
|
7 |
import logging
|
8 |
aws_access_key_id='AKIA2UC26WDCAFOF7X64'
|
9 |
aws_secret_access_key='4/EHQ3i2ci1seDA6OtxV6a4zaVz1W2uSZSrjjlFl'
|
10 |
-
aws_region = "eu-
|
11 |
#%%
|
12 |
from anthropic import AnthropicBedrock
|
13 |
|
@@ -24,91 +24,14 @@ client = AnthropicBedrock(
|
|
24 |
aws_region=aws_region,
|
25 |
)
|
26 |
modelid='anthropic.claude-v2:1'
|
|
|
27 |
message = client.messages.create(
|
28 |
model=modelid,
|
29 |
max_tokens=256,
|
30 |
messages=[{"role": "user", "content": "Hello, world"}]
|
31 |
)
|
32 |
print(message.content)
|
33 |
-
#%%
|
34 |
-
|
35 |
-
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
36 |
-
# SPDX-License-Identifier: Apache-2.0
|
37 |
-
"""
|
38 |
-
Shows how to generate a message with Anthropic Claude (on demand).
|
39 |
-
"""
|
40 |
-
import boto3
|
41 |
-
import json
|
42 |
-
import logging
|
43 |
-
|
44 |
-
from botocore.exceptions import ClientError
|
45 |
-
|
46 |
-
|
47 |
-
logger = logging.getLogger(__name__)
|
48 |
-
logging.basicConfig(level=logging.INFO)
|
49 |
-
|
50 |
-
def generate_message(bedrock_runtime, model_id, system_prompt, messages, max_tokens):
|
51 |
-
|
52 |
-
body=json.dumps(
|
53 |
-
{
|
54 |
-
"anthropic_version": "bedrock-2023-05-31",
|
55 |
-
"max_tokens": max_tokens,
|
56 |
-
"system": system_prompt,
|
57 |
-
"messages": messages
|
58 |
-
}
|
59 |
-
)
|
60 |
-
|
61 |
-
|
62 |
-
response = bedrock_runtime.invoke_model(body=body, modelId=model_id)
|
63 |
-
response_body = json.loads(response.get('body').read())
|
64 |
-
|
65 |
-
return response_body
|
66 |
-
|
67 |
-
def main():
|
68 |
-
"""
|
69 |
-
Entrypoint for Anthropic Claude message example.
|
70 |
-
"""
|
71 |
-
|
72 |
-
try:
|
73 |
-
# Define your credentials here (temporary for dev/testing only)
|
74 |
-
|
75 |
-
aws_region = "eu-central-1" # or the correct region
|
76 |
-
|
77 |
-
# Use boto3 session to inject credentials
|
78 |
-
session = boto3.Session(
|
79 |
-
aws_access_key_id=aws_access_key_id,
|
80 |
-
aws_secret_access_key=aws_secret_access_key,
|
81 |
-
region_name=aws_region
|
82 |
-
)
|
83 |
-
|
84 |
-
bedrock_runtime = session.client(service_name='bedrock-runtime')
|
85 |
-
|
86 |
-
model_id = 'anthropic.claude-v2:1'
|
87 |
-
system_prompt = "Please respond only with emoji."
|
88 |
-
max_tokens = 1000
|
89 |
-
|
90 |
-
# Prompt with user turn only.
|
91 |
-
user_message = {"role": "user", "content": "Hello World"}
|
92 |
-
messages = [user_message]
|
93 |
-
|
94 |
-
response = generate_message(bedrock_runtime, model_id, system_prompt, messages, max_tokens)
|
95 |
-
print("User turn only.")
|
96 |
-
print(json.dumps(response, indent=4))
|
97 |
-
|
98 |
-
# Prompt with both user and assistant messages
|
99 |
-
assistant_message = {"role": "assistant", "content": "<emoji>"}
|
100 |
-
messages = [user_message, assistant_message]
|
101 |
-
response = generate_message(bedrock_runtime, model_id, system_prompt, messages, max_tokens)
|
102 |
-
print("User turn and prefilled assistant response.")
|
103 |
-
print(json.dumps(response, indent=4))
|
104 |
-
|
105 |
-
except ClientError as err:
|
106 |
-
message = err.response["Error"]["Message"]
|
107 |
-
logger.error("A client error occurred: %s", message)
|
108 |
-
print("A client error occurred: " + format(message))
|
109 |
-
|
110 |
|
111 |
-
main()
|
112 |
|
113 |
#%%
|
114 |
from botocore.exceptions import ClientError
|
@@ -205,4 +128,88 @@ except ClientError as e:
|
|
205 |
print(f"❌ Unexpected error: {e}")
|
206 |
except Exception as e:
|
207 |
print(f"❌ General error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
#%%
|
|
|
7 |
import logging
|
8 |
aws_access_key_id='AKIA2UC26WDCAFOF7X64'
|
9 |
aws_secret_access_key='4/EHQ3i2ci1seDA6OtxV6a4zaVz1W2uSZSrjjlFl'
|
10 |
+
aws_region = "eu-north-1"
|
11 |
#%%
|
12 |
from anthropic import AnthropicBedrock
|
13 |
|
|
|
24 |
aws_region=aws_region,
|
25 |
)
|
26 |
modelid='anthropic.claude-v2:1'
|
27 |
+
model_id='eu.anthropic.claude-3-7-sonnet-20250219-v1:0'
|
28 |
message = client.messages.create(
|
29 |
model=modelid,
|
30 |
max_tokens=256,
|
31 |
messages=[{"role": "user", "content": "Hello, world"}]
|
32 |
)
|
33 |
print(message.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
|
|
35 |
|
36 |
#%%
|
37 |
from botocore.exceptions import ClientError
|
|
|
128 |
print(f"❌ Unexpected error: {e}")
|
129 |
except Exception as e:
|
130 |
print(f"❌ General error: {e}")
|
131 |
+
#%%
|
132 |
+
|
133 |
+
|
134 |
+
AWS_ACCESS_KEY_ID='AKIA2UC26WDCAFOF7X64'
|
135 |
+
AWS_SECRET_ACCESS_KEY='4/EHQ3i2ci1seDA6OtxV6a4zaVz1W2uSZSrjjlFl'
|
136 |
+
aws_region = "eu-west-1"
|
137 |
+
|
138 |
+
|
139 |
+
#%%
|
140 |
+
AWS_ACCESS_KEY_ID='AKIA2UC26WDCAFOF7X64'
|
141 |
+
AWS_SECRET_ACCESS_KEY='4/EHQ3i2ci1seDA6OtxV6a4zaVz1W2uSZSrjjlFl'
|
142 |
+
aws_region = "eu-west-1"
|
143 |
+
|
144 |
+
from langchain_aws import BedrockLLM
|
145 |
+
modelid='anthropic.claude-v2:1'
|
146 |
+
modelid="mistral.mistral-large-2402-v1:0"
|
147 |
+
#model_id='eu.anthropic.claude-3-7-sonnet-20250219-v1:0'
|
148 |
+
custom_llm = BedrockLLM(
|
149 |
+
aws_access_key_id = AWS_ACCESS_KEY_ID,
|
150 |
+
aws_secret_access_key = AWS_SECRET_ACCESS_KEY,
|
151 |
+
|
152 |
+
region_name = aws_region,
|
153 |
+
|
154 |
+
# which Bedrock “provider” you’re talking to:
|
155 |
+
# – use "anthropic" for Claude models
|
156 |
+
# – use "cohere" for the Cohere models
|
157 |
+
provider = "mistral",
|
158 |
+
|
159 |
+
model_id = modelid,
|
160 |
+
model_kwargs = {"temperature": 0.7},
|
161 |
+
streaming = True,
|
162 |
+
)
|
163 |
+
|
164 |
+
print(custom_llm.invoke("What’s the recipe for mayonnaise?"))
|
165 |
+
|
166 |
+
#%%
|
167 |
+
from langchain_aws import ChatBedrockConverse
|
168 |
+
import os
|
169 |
+
system_prompt = (
|
170 |
+
"Du bist DevalBot, ein konversationeller Assistent des Deutschen Evaluierungsinstituts "
|
171 |
+
"für Entwicklungsbewertung (DEval). DEval bietet staatlichen und zivilgesellschaftlichen "
|
172 |
+
"Organisationen in der Entwicklungszusammenarbeit unabhängige und wissenschaftlich fundierte "
|
173 |
+
"Evaluierungen. Deine Hauptsprache ist Deutsch; antworte daher standardmäßig auf Deutsch. "
|
174 |
+
"Du kannst zudem bei statistischen Analysen und Programmierung in Stata und R unterstützen."
|
175 |
+
)
|
176 |
+
#%%
|
177 |
+
# Initialize the streaming Bedrock chat model
|
178 |
+
bedrock_llm = ChatBedrockConverse(
|
179 |
+
aws_access_key_id=AWS_ACCESS_KEY_ID,
|
180 |
+
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
|
181 |
+
region_name=os.environ.get("AWS_DEFAULT_REGION", "eu-west-1"),
|
182 |
+
model_id="mistral.mistral-large-2402-v1:0", # or your preferred Bedrock model
|
183 |
+
temperature= 0.7)
|
184 |
+
#%%
|
185 |
+
from bedrock_client import bedrock_llm, system_prompt
|
186 |
+
from langchain.schema import SystemMessage, HumanMessage, AIMessage
|
187 |
+
|
188 |
+
|
189 |
+
|
190 |
+
def build_messages(
|
191 |
+
user_message: str,
|
192 |
+
history: list[dict]) -> list:
|
193 |
+
|
194 |
+
messages: list = []
|
195 |
+
|
196 |
+
# 1) Add the system prompt first
|
197 |
+
messages.append(SystemMessage(content=system_prompt))
|
198 |
+
|
199 |
+
# 2) Walk the history and map to HumanMessage or AIMessage
|
200 |
+
for msg in history:
|
201 |
+
if msg["role"] == "user":
|
202 |
+
messages.append(HumanMessage(content=msg["content"]))
|
203 |
+
elif msg["role"] == "assistant":
|
204 |
+
messages.append(AIMessage(content=msg["content"]))
|
205 |
+
else:
|
206 |
+
# you can choose to ignore or log unexpected roles
|
207 |
+
continue
|
208 |
+
|
209 |
+
# 3) Finally, append the new user message
|
210 |
+
messages.append(HumanMessage(content=user_message))
|
211 |
+
return messages
|
212 |
+
|
213 |
+
|
214 |
+
build_messages('hi',[])
|
215 |
#%%
|
test copy.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#%%
|
2 |
+
from langchain.schema import SystemMessage, HumanMessage, AIMessage
|
3 |
+
|
4 |
+
|
5 |
+
# Define your system prompt as a plain string
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
def build_messages(
|
10 |
+
user_message: str,
|
11 |
+
history: list[dict]) -> list:
|
12 |
+
|
13 |
+
system_prompt = "Du bist DevalBot, ein konversationeller Assistent des Deutschen Evaluierungsinstituts " \
|
14 |
+
"für Entwicklungsbewertung (DEval). DEval bietet staatlichen und zivilgesellschaftlichen " \
|
15 |
+
"Organisationen in der Entwicklungszusammenarbeit unabhängige und wissenschaftlich fundierte " \
|
16 |
+
"Evaluierungen. Deine Hauptsprache ist Deutsch; antworte daher standardmäßig auf Deutsch. " \
|
17 |
+
"Du kannst zudem bei statistischen Analysen und Programmierung in Stata und R unterstützen."
|
18 |
+
|
19 |
+
messages: list = []
|
20 |
+
|
21 |
+
# 1) Add the system prompt first
|
22 |
+
messages.append(SystemMessage(content=system_prompt))
|
23 |
+
|
24 |
+
# 2) Walk the history and map to HumanMessage or AIMessage
|
25 |
+
for msg in history:
|
26 |
+
if msg["role"] == "user":
|
27 |
+
messages.append(HumanMessage(content=msg["content"]))
|
28 |
+
elif msg["role"] == "assistant":
|
29 |
+
messages.append(AIMessage(content=msg["content"]))
|
30 |
+
else:
|
31 |
+
# you can choose to ignore or log unexpected roles
|
32 |
+
continue
|
33 |
+
|
34 |
+
# 3) Finally, append the new user message
|
35 |
+
messages.append(HumanMessage(content=user_message))
|
36 |
+
return messages
|
37 |
+
|
38 |
+
|
39 |
+
build_messages('hi',[])
|
40 |
+
#%%
|