Skip to content

Instantly share code, notes, and snippets.

@harishkotra
Created November 28, 2024 03:54
Show Gist options
  • Save harishkotra/1e973896c784d41c5811fbd01d7ceee7 to your computer and use it in GitHub Desktop.
Save harishkotra/1e973896c784d41c5811fbd01d7ceee7 to your computer and use it in GitHub Desktop.
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
import json
# Initialize the chat model with Gaia's endpoint
llm = ChatOpenAI(
model="llama3b",
openai_api_key="gaia",
openai_api_base="https://llama3b.gaia.domains/v1",
temperature=0.7
)
# Create prompt templates for different tasks
translation_prompt = ChatPromptTemplate.from_messages([
("system", "You are a translator. Translate the text from English to French."),
("human", "{text}")
])
summarization_prompt = ChatPromptTemplate.from_messages([
("system", "You are a summarizer. Summarize the following French text in English in one sentence."),
("human", "{text}")
])
analysis_prompt = ChatPromptTemplate.from_messages([
("system", "You are a text analyzer. Analyze the following summary and provide three key points in JSON format with 'key_points' as the key."),
("human", "{text}")
])
# Create individual chains
translation_chain = translation_prompt | llm | StrOutputParser()
summarization_chain = summarization_prompt | llm | StrOutputParser()
analysis_chain = analysis_prompt | llm | StrOutputParser()
# Create a combined chain
def process_text(input_text: str) -> dict:
# First, translate to French
french_text = translation_chain.invoke({"text": input_text})
print(f"\nTranslated to French:\n{french_text}")
# Then, summarize back to English
summary = summarization_chain.invoke({"text": french_text})
print(f"\nSummarized in English:\n{summary}")
# Finally, analyze the summary
analysis = analysis_chain.invoke({"text": summary})
try:
# Try to parse JSON response
analysis_dict = json.loads(analysis)
except json.JSONDecodeError:
# If JSON parsing fails, return raw text
analysis_dict = {"key_points": analysis}
print(f"\nAnalysis:\n{json.dumps(analysis_dict, indent=2)}")
return {
"french_translation": french_text,
"english_summary": summary,
"analysis": analysis_dict
}
# Example usage
if __name__ == "__main__":
input_text = """
The rise of artificial intelligence has transformed many industries,
creating new opportunities for innovation and efficiency. However,
it also presents challenges regarding ethics, privacy, and job displacement
that society must carefully address.
"""
print("Original text:", input_text.strip())
result = process_text(input_text)
# Show how to access specific parts of the result
print("\nAccessing specific parts of the result:")
print(f"French translation first 50 chars: {result['french_translation'][:50]}...")
print(f"English summary first 50 chars: {result['english_summary'][:50]}...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment