tunght commited on
Commit
f60d311
·
1 Parent(s): 27ee5d4

add gemini

Browse files
Files changed (2) hide show
  1. app.py +24 -11
  2. requirements.txt +2 -1
app.py CHANGED
@@ -8,6 +8,7 @@ from langchain_core.output_parsers import JsonOutputParser
8
  from langchain_openai.chat_models import ChatOpenAI
9
  from langchain.schema import HumanMessage, SystemMessage, AIMessage
10
  from langchain_anthropic import ChatAnthropic, ChatAnthropicMessages
 
11
  from langchain_groq import ChatGroq
12
  import openai
13
 
@@ -20,6 +21,8 @@ from langchain_openai import OpenAIEmbeddings
20
  from langchain_text_splitters import RecursiveCharacterTextSplitter
21
  from langchain_core.vectorstores import VectorStoreRetriever
22
 
 
 
23
 
24
  feature_text = "Brand: Duckly. \nProduct name: Duck runner pro. \nKey properties: t-shirt, for running, sweat wicking, for marathon, 100% cotton."
25
 
@@ -85,7 +88,8 @@ models = ["gpt-4-turbo",
85
  "gpt-3.5-turbo",
86
  "claude-3-sonnet-20240229",
87
  "claude-3-opus-20240229",
88
- "claude-3-5-sonnet-20240620"
 
89
  #"llama3-70b-8192",
90
  ]
91
 
@@ -229,13 +233,22 @@ def get_language(struct_lang, copy_lang):
229
  return "English"
230
 
231
 
232
- def get_model(model_name):
 
 
 
 
 
 
 
233
  if model_name.startswith("gpt"):
234
- chat = ChatOpenAI(model=model_name, max_tokens=4096)
235
  elif model_name.startswith("claude"):
236
- chat = ChatAnthropic(model_name=model_name, anthropic_api_key=os.environ["ANTHROPIC_API_KEY"], max_tokens_to_sample=4096)
 
 
237
  else:
238
- chat = ChatGroq(model_name=model_name, api_key=os.environ["GROQ_API_KEY"])
239
  return chat
240
 
241
 
@@ -309,7 +322,7 @@ def generate(*data):
309
  if glossary_upload is not None:
310
  glossary = build_glossary(glossary_upload)
311
 
312
- chat = get_model(model)
313
 
314
  types, struct_languages = parse_structure(struct_ref)
315
  copy_languages = detect_language([struct_ref[2 * i + 1] for i in range(visible + 1)], model=chat)
@@ -363,7 +376,7 @@ Do not hallucinate any information.
363
  Use creative language in each output, do not use the common ways of starting product descriptions.
364
  Avoid common phrases and cliches such as "Step into something", "Elevate something", "Discover something", "Unleash something", "Embrace something", and similar phrases.
365
  For each version, try to write in different style.
366
- Put each item in the list on a separate line. Use <br> tag to separate items in the list.
367
  Rate the quality of each version based on the following criteria:
368
  - how well it follows the reference structure.
369
  - how well it follows the tone of voice, rythm, cadence and style of the reference copy.
@@ -427,7 +440,7 @@ Do not hallucinate any information.
427
  Use creative language in each output, do not use the common ways of starting product descriptions.
428
  Avoid common phrases and cliches such as "Step into something", "Elevate something", "Discover something", "Unleash something", "Embrace something", and similar phrases.
429
  For each version, try to write in different style.
430
- Put each item in the list on a separate line. Use <br> tag to separate items in the list.
431
  Rate the quality of each version based on the following criteria:
432
  - how well it follows the tone of voice, rythm, cadence and style of the reference copy.
433
  - how faithful it describes the product features.
@@ -490,7 +503,7 @@ Do not hallucinate any information.
490
  Use creative language in each output, do not use the common ways of starting product descriptions.
491
  Avoid common phrases and cliches such as "Step into something", "Elevate something", "Discover something", "Unleash something", "Embrace something", and similar phrases.
492
  For each version, try to write in different style.
493
- Put each item in the list on a separate line. Use <br> tag to separate items in the list.
494
  Rate the quality of each version based on the following criteria:
495
  - how well it follows the reference structure.
496
  - how faithful it describes the product features.
@@ -538,7 +551,7 @@ Excluded words: {excluded_words}"""),]
538
 
539
  descriptions = ""
540
 
541
- response = chat.batch(batch, temperature=temperature)
542
  print(response)
543
 
544
  parser = JsonOutputParser()
@@ -549,7 +562,7 @@ Excluded words: {excluded_words}"""),]
549
  bestid = jr["best_version"]["id"]
550
  for d in jr["versions"]:
551
  if d["id"] == bestid:
552
- bestd = d["content"] + (f"\n\nDebug info:\n\nScore: {d['score']}\n\nExplanation: {jr['best_version']['explanation']}" if debug else "")
553
  bests = d["score"]
554
  break
555
  # bests = 0
 
8
  from langchain_openai.chat_models import ChatOpenAI
9
  from langchain.schema import HumanMessage, SystemMessage, AIMessage
10
  from langchain_anthropic import ChatAnthropic, ChatAnthropicMessages
11
+ from langchain_google_genai import ChatGoogleGenerativeAI
12
  from langchain_groq import ChatGroq
13
  import openai
14
 
 
21
  from langchain_text_splitters import RecursiveCharacterTextSplitter
22
  from langchain_core.vectorstores import VectorStoreRetriever
23
 
24
+ import time
25
+
26
 
27
  feature_text = "Brand: Duckly. \nProduct name: Duck runner pro. \nKey properties: t-shirt, for running, sweat wicking, for marathon, 100% cotton."
28
 
 
88
  "gpt-3.5-turbo",
89
  "claude-3-sonnet-20240229",
90
  "claude-3-opus-20240229",
91
+ "claude-3-5-sonnet-20240620",
92
+ "gemini-1.5-pro"
93
  #"llama3-70b-8192",
94
  ]
95
 
 
233
  return "English"
234
 
235
 
236
+ def post_process(text):
237
+ # text = text.strip().replace('<br>', ' ')
238
+ with open(f"postprocess_{time.time()}.txt", "w") as f:
239
+ f.write(text)
240
+ return text
241
+
242
+
243
+ def get_model(model_name, temperature=0.0):
244
  if model_name.startswith("gpt"):
245
+ chat = ChatOpenAI(model=model_name, max_tokens=4096, temperature=temperature)
246
  elif model_name.startswith("claude"):
247
+ chat = ChatAnthropic(model_name=model_name, anthropic_api_key=os.environ["ANTHROPIC_API_KEY"], max_tokens_to_sample=4096, temperature=temperature)
248
+ elif model_name.startswith("gemini"):
249
+ chat = ChatGoogleGenerativeAI(model=model_name, api_key=os.environ["GOOGLE_API_KEY"], temperature=temperature)
250
  else:
251
+ chat = ChatGroq(model_name=model_name, api_key=os.environ["GROQ_API_KEY"], temperature=temperature)
252
  return chat
253
 
254
 
 
322
  if glossary_upload is not None:
323
  glossary = build_glossary(glossary_upload)
324
 
325
+ chat = get_model(model, temperature=temperature)
326
 
327
  types, struct_languages = parse_structure(struct_ref)
328
  copy_languages = detect_language([struct_ref[2 * i + 1] for i in range(visible + 1)], model=chat)
 
376
  Use creative language in each output, do not use the common ways of starting product descriptions.
377
  Avoid common phrases and cliches such as "Step into something", "Elevate something", "Discover something", "Unleash something", "Embrace something", and similar phrases.
378
  For each version, try to write in different style.
379
+ Put each item in the list on a separate line.
380
  Rate the quality of each version based on the following criteria:
381
  - how well it follows the reference structure.
382
  - how well it follows the tone of voice, rythm, cadence and style of the reference copy.
 
440
  Use creative language in each output, do not use the common ways of starting product descriptions.
441
  Avoid common phrases and cliches such as "Step into something", "Elevate something", "Discover something", "Unleash something", "Embrace something", and similar phrases.
442
  For each version, try to write in different style.
443
+ Put each item in the list on a separate line.
444
  Rate the quality of each version based on the following criteria:
445
  - how well it follows the tone of voice, rythm, cadence and style of the reference copy.
446
  - how faithful it describes the product features.
 
503
  Use creative language in each output, do not use the common ways of starting product descriptions.
504
  Avoid common phrases and cliches such as "Step into something", "Elevate something", "Discover something", "Unleash something", "Embrace something", and similar phrases.
505
  For each version, try to write in different style.
506
+ Put each item in the list on a separate line.
507
  Rate the quality of each version based on the following criteria:
508
  - how well it follows the reference structure.
509
  - how faithful it describes the product features.
 
551
 
552
  descriptions = ""
553
 
554
+ response = chat.batch(batch)
555
  print(response)
556
 
557
  parser = JsonOutputParser()
 
562
  bestid = jr["best_version"]["id"]
563
  for d in jr["versions"]:
564
  if d["id"] == bestid:
565
+ bestd = post_process(d["content"]) + (f"\n\nDebug info:\n\nScore: {d['score']}\n\nExplanation: {jr['best_version']['explanation']}" if debug else "")
566
  bests = d["score"]
567
  break
568
  # bests = 0
requirements.txt CHANGED
@@ -7,4 +7,5 @@ langchain_groq
7
  bcrypt
8
  langchain_chroma
9
  langchainhub
10
- langchain_community
 
 
7
  bcrypt
8
  langchain_chroma
9
  langchainhub
10
+ langchain_community
11
+ langchain-google-genai