Skip to content

Commit

Permalink
tutorial104
Browse files Browse the repository at this point in the history
  • Loading branch information
ronidas39 committed Jul 13, 2024
1 parent 1a7ccc6 commit 49fd3c6
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions tutorial104/main.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import boto3

#pip3 install boto3
import boto3,json
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate

client=boto3.resource("dynamodb",region_name="us-east-1")
table=client.Table("token_logs")

from datetime import datetime
def update_table(item):
client=boto3.resource("dynamodb")
table=client.Table("token_logs")
table.put_item(Item=item)
llm=ChatOpenAI(model="gpt-4o")
template="""
you are intelligent assistant who can write any article on {topic} with nice and attractive lines.
output will only article nothinbg extra
template="""you are intelligent assistant who can answer any {question} give by the user
response must be a json only , with the following keys,nothing else:
qsn:
ans:
"""
prompt=PromptTemplate.from_template(template)
chain=prompt|llm
response=chain.invoke({"topic":"Indian politics"})
response=chain.invoke({"question":"write a story on king james"})
answer=response.content
response_id=response.id
data=response.usage_metadata
data.update({"response_id":response_id,"answer":response.content,"question":template})
table.put_item(Item=data)

answer=answer.replace("json","")
answer=answer.replace("`","")
answer=json.loads(answer)
metadata=response.usage_metadata
metadata["response_id"]=response.id
metadata["qsn"]=answer["qsn"]
metadata["ans"]=answer["ans"]
metadata["timestamp"]=str(datetime.now())
update_table(metadata)

0 comments on commit 49fd3c6

Please sign in to comment.