File size: 12,952 Bytes
bc09c89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38b6ee6
15773f6
bc09c89
215e74e
15773f6
bc09c89
 
 
5612d16
bc09c89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d94499
b2c6609
 
 
 
15773f6
0b06d6e
 
 
8c6192c
0b06d6e
 
 
 
 
 
 
4d94499
0b06d6e
 
 
8c6192c
0b06d6e
15773f6
 
 
4d94499
0b06d6e
4d94499
0b06d6e
88657de
4d94499
 
 
0b06d6e
 
 
4d94499
 
15773f6
bc09c89
 
 
15773f6
bc09c89
15773f6
bc09c89
 
 
 
 
 
 
 
 
d083506
 
bc09c89
15773f6
 
 
bc09c89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdcef72
15773f6
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# import os
# import streamlit as st
# import torch
# from langchain.chains import LLMChain
# from langchain.prompts import ChatPromptTemplate
# from langchain_huggingface import HuggingFaceEndpoint

# def create_conversation_prompt(name1: str, name2: str, persona_style: str):
#     """
#     Create a prompt that instructs the model to produce exactly 15 messages
#     of conversation, alternating between name1 and name2, starting with name1.

#     We will be very explicit and not allow any formatting except the required lines.
#     """
#     prompt_template_str = f"""
#     You are simulating a conversation of exactly 15 messages between two people: {name1} and {name2}.
#     {name1} speaks first (message 1), then {name2} (message 2), then {name1} (message 3), and so forth,
#     alternating until all 15 messages are complete. The 15th message is by {name1}.

#     Requirements:
#     - Output exactly 15 lines, no more, no less.
#     - Each line must be a single message in the format:
#       {name1}: <message> or {name2}: <message>
#     - Do not add any headings, numbers, sample outputs, or explanations.
#     - Do not mention code, programming, or instructions.
#     - Each message should be 1-2 short sentences, friendly, natural, reflecting the style: {persona_style}.
#     - Use everyday language, can ask questions, show opinions.
#     - Use emojis sparingly if it fits the style (no more than 1-2 total).
#     - No repeated lines, each message should logically follow from the previous one.
#     - Do not produce anything after the 15th message. No extra lines or text.

#     Produce all 15 messages now:
#     """
#     return ChatPromptTemplate.from_template(prompt_template_str)

# def create_summary_prompt(name1: str, name2: str, conversation: str):
#     """Prompt for generating a title and summary."""
#     summary_prompt_str = f"""
#     Below is a completed 15-message conversation between {name1} and {name2}:

#     {conversation}

#     Please provide:
#     Title: <A short descriptive title of the conversation>
#     Summary: <A few short sentences highlighting the main points, tone, and conclusion>

#     Do not continue the conversation, do not repeat it, and do not add extra formatting beyond the two lines:
#     - One line starting with "Title:"
#     - One line starting with "Summary:"
#     """
#     return ChatPromptTemplate.from_template(summary_prompt_str)

# def main():
#     st.title("LLM Conversation Simulation")

#     model_names = [
#         "meta-llama/Llama-3.3-70B-Instruct",
#         "meta-llama/Llama-3.1-405B-Instruct",
#         "Qwen/Qwen2.5-72B-Instruct",
#         "deepseek-ai/DeepSeek-V3",
#         "deepseek-ai/DeepSeek-V2.5"
        
#     ]
#     selected_model = st.selectbox("Select a model:", model_names)

#     name1 = st.text_input("Enter the first user's name:", value="Alice")
#     name2 = st.text_input("Enter the second user's name:", value="Bob")
#     persona_style = st.text_area("Enter the persona style characteristics:", 
#                                  value="friendly, curious, and a bit sarcastic")

#     if st.button("Start Conversation Simulation"):
#         st.write("**Loading model...**")
#         print("Loading model...")

#         with st.spinner("Starting simulation..."):
#             endpoint_url = f"https://api-inference.huggingface.co/models/{selected_model}"

#             try:
#                 llm = HuggingFaceEndpoint(
#                     endpoint_url=endpoint_url,
#                     huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN"),
#                     task="text-generation",
#                     temperature=0.7,
#                     max_new_tokens=512
#                 )
#                 st.write("**Model loaded successfully!**")
#                 print("Model loaded successfully!")
#             except Exception as e:
#                 st.error(f"Error initializing HuggingFaceEndpoint: {e}")
#                 print(f"Error initializing HuggingFaceEndpoint: {e}")
#                 return

#             conversation_prompt = create_conversation_prompt(name1, name2, persona_style)
#             conversation_chain = LLMChain(llm=llm, prompt=conversation_prompt)

#             st.write("**Generating the full 15-message conversation...**")
#             print("Generating the full 15-message conversation...")

#             try:
#                 # Generate all 15 messages in one go
#                 conversation = conversation_chain.run(chat_history="", input="").strip()

#                 st.subheader("Final Conversation:")
#                 st.text(conversation)
#                 print("Conversation Generation Complete.\n")
#                 print("Full Conversation:\n", conversation)

#                 # Summarize the conversation
#                 summary_prompt = create_summary_prompt(name1, name2, conversation)
#                 summary_chain = LLMChain(llm=llm, prompt=summary_prompt)

#                 st.subheader("Summary and Title:")
#                 st.write("**Summarizing the conversation...**")
#                 print("Summarizing the conversation...")

#                 summary = summary_chain.run(chat_history="", input="")
#                 st.write(summary)
#                 print("Summary:\n", summary)

#             except Exception as e:
#                 st.error(f"Error generating conversation: {e}")
#                 print(f"Error generating conversation: {e}")

# if __name__ == "__main__":
#     main()


import os
import streamlit as st
import google.cloud.aiplatform as aiplatform
from langchain.chains import LLMChain
from langchain.prompts import ChatPromptTemplate
from langchain.llms.base import LLM
from pydantic import BaseModel
from typing import Optional, List, Mapping, Any

###############################################################################
# 1. Create a Custom LLM class for LangChain to call your Vertex AI endpoint.
###############################################################################
class VertexAICustomModel(LLM, BaseModel):
    project_id: str
    location: str
    endpoint_id: str
    temperature: float = 0.7
    max_new_tokens: int = 512
    
    @property
    def _llm_type(self) -> str:
        return "vertex_ai_custom"

    def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
        # Initialize Vertex AI with your project/region
        aiplatform.init(project=self.project_id, location=self.location)
        endpoint = aiplatform.Endpoint(
            endpoint_name=f"projects/{self.project_id}/locations/{self.location}/endpoints/{self.endpoint_id}"
        )
        
        # Construct the instance for prediction. 
        # NOTE: Adjust 'prompt', 'temperature', etc. if your model expects different parameters.
        instance = {
            "prompt": prompt,
            "temperature": self.temperature,
            "max_new_tokens": self.max_new_tokens
        }

        # Call the endpoint
        response = endpoint.predict(instances=[instance])
        
        # Extract the text from the response.
        # This will vary depending on how your model returns predictions.
        # A common approach is response.predictions[0]["generated_text"], 
        # but confirm your model's actual JSON structure.
        predictions = response.predictions
        if not predictions or "generated_text" not in predictions[0]:
            raise ValueError(
                f"Unexpected response structure from Vertex AI endpoint: {response}"
            )
        
        text = predictions[0]["generated_text"]

        # Optionally apply 'stop' tokens
        if stop:
            for s in stop:
                if s in text:
                    text = text.split(s)[0]
        return text

    @property
    def _identifying_params(self) -> Mapping[str, Any]:
        """Return any identifying parameters of this LLM."""
        return {
            "endpoint_id": self.endpoint_id,
            "project_id": self.project_id,
            "location": self.location,
            "temperature": self.temperature,
            "max_new_tokens": self.max_new_tokens,
        }

###############################################################################
# 2. Create your conversation and summary prompt templates (unchanged).
###############################################################################
def create_conversation_prompt(name1: str, name2: str, persona_style: str):
    """
    Create a prompt that instructs the model to produce exactly 15 messages
    of conversation, alternating between name1 and name2, starting with name1.
    """
    prompt_template_str = f"""
    You are simulating a conversation of exactly 15 messages between two people: {name1} and {name2}.
    {name1} speaks first (message 1), then {name2} (message 2), then {name1} (message 3), and so forth,
    alternating until all 15 messages are complete. The 15th message is by {name1}.

    Requirements:
    - Output exactly 15 lines, no more, no less.
    - Each line must be a single message in the format:
      {name1}: <message> or {name2}: <message>
    - Do not add any headings, numbers, sample outputs, or explanations.
    - Do not mention code, programming, or instructions.
    - Each message should be 1-2 short sentences, friendly, natural, reflecting the style: {persona_style}.
    - Use everyday language, can ask questions, show opinions.
    - Use emojis sparingly if it fits the style (no more than 1-2 total).
    - No repeated lines, each message should logically follow from the previous one.
    - Do not produce anything after the 15th message. No extra lines or text.

    Produce all 15 messages now:
    """
    return ChatPromptTemplate.from_template(prompt_template_str)

def create_summary_prompt(name1: str, name2: str, conversation: str):
    """Prompt for generating a title and summary."""
    summary_prompt_str = f"""
    Below is a completed 15-message conversation between {name1} and {name2}:
    {conversation}
    Please provide:
    Title: <A short descriptive title of the conversation>
    Summary: <A few short sentences highlighting the main points, tone, and conclusion>
    Do not continue the conversation, do not repeat it, and do not add extra formatting beyond the two lines:
    - One line starting with "Title:"
    - One line starting with "Summary:"
    """
    return ChatPromptTemplate.from_template(summary_prompt_str)

###############################################################################
# 3. Main Streamlit app with Vertex AI usage.
###############################################################################
def main():
    st.title("LLM Conversation Simulation (GCP Vertex AI)")

    # We can remove model selection if we are always using your deployed model:
    # st.selectbox(... ) # => Removed

    # Hardcode or load your Vertex AI endpoint details here
    project_id = "282802344966"
    location = "us-west1"
    endpoint_id = "1106913540054188032"

    # Input fields for conversation
    name1 = st.text_input("Enter the first user's name:", value="Alice")
    name2 = st.text_input("Enter the second user's name:", value="Bob")
    persona_style = st.text_area("Enter the persona style characteristics:",
                                 value="friendly, curious, and a bit sarcastic")

    if st.button("Start Conversation Simulation"):
        st.write("**Initializing Vertex AI endpoint...**")
        st.spinner("Starting simulation...")

        # Create your custom LLM that calls Vertex AI
        llm = VertexAICustomModel(
            project_id=project_id,
            location=location,
            endpoint_id=endpoint_id,
            temperature=0.7,
            max_new_tokens=512
        )

        st.write("**Vertex AI endpoint loaded successfully!**")

        # Build the conversation chain
        conversation_prompt = create_conversation_prompt(name1, name2, persona_style)
        conversation_chain = LLMChain(llm=llm, prompt=conversation_prompt)

        st.write("**Generating the full 15-message conversation...**")

        try:
            # Generate all 15 messages in one go
            conversation = conversation_chain.run(chat_history="", input="").strip()

            st.subheader("Final Conversation:")
            st.text(conversation)

            # Summarize the conversation
            summary_prompt = create_summary_prompt(name1, name2, conversation)
            summary_chain = LLMChain(llm=llm, prompt=summary_prompt)

            st.subheader("Summary and Title:")
            st.write("**Summarizing the conversation...**")

            summary = summary_chain.run(chat_history="", input="")
            st.write(summary)

        except Exception as e:
            st.error(f"Error generating conversation: {e}")
            print(f"Error generating conversation: {e}")

if __name__ == "__main__":
    main()