Spaces:
Runtime error
Runtime error
File size: 5,808 Bytes
22d9697 |
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 |
import os
from langchain_openai import ChatOpenAI
from dotenv import load_dotenv
import json
import openai
from langsmith.wrappers import wrap_openai
from langsmith import traceable
from langchain import hub
import re
load_dotenv()
# --------------------------------------------------
# Utility Functions
# --------------------------------------------------
client = wrap_openai(openai.OpenAI())
chatModel_json = ChatOpenAI(
model="gpt-4o",
model_kwargs={"response_format": {"type": "json_object"}},
)
chatModel_no_json = ChatOpenAI(
model="gpt-4o"
)
async def parse_json_with_retries(json_string, max_attempts=3):
for attempt in range(max_attempts):
try:
return json.loads(json_string)
except json.JSONDecodeError as error:
if attempt == max_attempts - 1:
raise error
@traceable
async def invoke_agent(runnable, params):
content = runnable.invoke(params)
if hasattr(content, 'content'):
content = content.content
return content
# --------------------------------------------------
# Translation Function
# --------------------------------------------------
@traceable
async def translate_text(text, target_language):
try:
first_prompt = hub.pull("getgloby/agent-1-get-info")
print("First prompt pulled successfully.")
except Exception as e:
print(f"Error al obtener el primer agente: {e}")
return
first_runnable = first_prompt | chatModel_no_json
try:
first_agent_output = await invoke_agent(first_runnable, {
"originalText": text,
"targetLanguage": target_language,
})
print(f"First agent output: {first_agent_output}")
except Exception as e:
print(f"Error invoking first agent: {e}")
print(f"Error details: {e}")
return
translations = []
for i in range(3):
try:
second_prompt = hub.pull("getgloby/agent-2-translate")
print(f"Second prompt pulled successfully for iteration {i + 1}.")
except Exception as e:
print(f"Error al obtener el segundo agente en la iteración {i + 1}: {e}")
continue
second_runnable = second_prompt | chatModel_json
try:
second_agent_output = await invoke_agent(second_runnable, {
"originalText": text,
"firstAgentOutput": first_agent_output,
"targetLanguage": target_language,
})
print(f"Second agent output (iteration {i + 1}): {second_agent_output}")
except Exception as e:
print(f"Error invoking second agent in iteration {i + 1}: {e}")
print(f"Error details: {e}")
continue
try:
parsed_second_agent_output = await parse_json_with_retries(second_agent_output)
translation = parsed_second_agent_output["translation"]
print(f"Parsed second agent output (iteration {i + 1}): {translation}")
except Exception as e:
print(f"Error parsing second agent output in iteration {i + 1}: {e}")
print(f"Error details: {e}")
continue
try:
third_prompt = hub.pull("getgloby/agent-3-improve-translation")
print(f"Third prompt pulled successfully for iteration {i + 1}.")
except Exception as e:
print(f"Error al obtener el tercer agente en la iteración {i + 1}: {e}")
continue
third_runnable = third_prompt | chatModel_json
try:
third_agent_output = await invoke_agent(third_runnable, {
"originalText": text,
"firstAgentOutput": first_agent_output,
"secondAgentOutput": translation,
})
print(f"Third agent output (iteration {i + 1}): {third_agent_output}")
except Exception as e:
print(f"Error invoking third agent in iteration {i + 1}: {e}")
print(f"Error details: {e}")
continue
try:
third_agent_output_json = await parse_json_with_retries(third_agent_output)
optimized_translation = third_agent_output_json["optimizedTranslation"]
print(f"Optimized translation (iteration {i + 1}): {optimized_translation}")
except Exception as e:
print(f"Error parsing third agent output in iteration {i + 1}: {e}")
print(f"Error details: {e}")
continue
translations.append(optimized_translation)
try:
final_prompt = hub.pull("getgloby/agent-4-final-translation")
print("Final prompt pulled successfully.")
except Exception as e:
print(f"Error al obtener el agente final: {e}")
return
final_runnable = final_prompt | chatModel_json
try:
final_agent_output = await invoke_agent(final_runnable, {
"originalText": text,
"firstAgentOutput": first_agent_output, # Cambiado de contextInfo a firstAgentOutput
"translation1": translations[0],
"translation2": translations[1],
"translation3": translations[2],
"targetLanguage": target_language,
})
print(f"Final agent output: {final_agent_output}")
except Exception as e:
print(f"Error invoking final agent: {e}")
print(f"Error details: {e}")
return
try:
final_agent_output_json = await parse_json_with_retries(final_agent_output)
final_translation = final_agent_output_json["finalTranslation"]
print(f"Final translation: {final_translation}")
except Exception as e:
print(f"Error parsing final agent output: {e}")
print(f"Error details: {e}")
return
return final_translation
|