Spaces:
Sleeping
Sleeping
import os | |
import platform | |
import openai | |
import chromadb | |
import langchain | |
from langchain.embeddings.openai import OpenAIEmbeddings | |
from langchain.vectorstores import Chroma | |
from langchain.text_splitter import TokenTextSplitter | |
from langchain.llms import OpenAI | |
from langchain.chat_models import ChatOpenAI | |
from langchain.chains import ChatVectorDBChain | |
from langchain.document_loaders import GutenbergLoader | |
from langchain.embeddings import LlamaCppEmbeddings | |
from langchain.llms import LlamaCpp | |
from langchain.output_parsers import StructuredOutputParser, ResponseSchema | |
from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate | |
from langchain.llms import OpenAI | |
from langchain.chains import LLMChain | |
from langchain.chains import SimpleSequentialChain | |
from langchain.output_parsers import PydanticOutputParser | |
from pydantic import BaseModel, Field, validator | |
from typing import List, Dict | |
# class AnswerTemplate(BaseModel): | |
# isComplete: bool = Field(description="Is the input complete?") | |
# answer: str = Field(description="""If the answer to 'isComplete' is true leave this empty, else respond to user's last message in a cordial manner and then ask the user for the missing information. Just one question.""") | |
class AnswerTemplate(BaseModel): | |
# isComplete: bool = Field(description="Is the input complete?") | |
answer: str = Field(description="Question that you asked") | |
class Response_Agent(): | |
def __init__(self): | |
self.model_name = "gpt-4" | |
self.model = OpenAI(model_name=self.model_name, temperature=0) | |
self.output_parser = PydanticOutputParser(pydantic_object=AnswerTemplate) | |
self.format_instructions = self.output_parser.get_format_instructions() | |
# self.prompt = PromptTemplate( | |
# template="""\ | |
# ### Instruction | |
# You are Trainline Mate an helpful assistant that plans tours for people at trainline.com. | |
# As a smart itinerary planner with extensive knowledge of places around the | |
# world, your task is to determine the user's travel destinations and any specific interests or preferences from | |
# their message. | |
# ### Your task | |
# Is this input complete? If not, what is missing? | |
# ### If something is missing then ask for the missing information. | |
# Don't ask more then one question. | |
# Ask just one of the following: | |
# If 'type' is empty then ask the user what type of the trip are you planning and with whom are you travelling?; | |
# If 'where' is empty then ask the user where is they going to travel to?; | |
# If 'start_date' is empty then ask the user what is the start date?; | |
# If 'end_date' is empty then ask the user what is the end date?; | |
# If 'time_constrains' is empty then ask the user if is there any time constrains that should be considered?; | |
# If 'preferences' is empty then ask the user if they have thought about any activities you want to do while you're there?; | |
# If 'conditions' is empty then ask the user if they have any special medical condition?; | |
# If 'dist_range' is empty then ask the user what is the distance range you prefer for your ativities? \n### Input: {input} | |
# \n### Response: {format_instructions} | |
# """, | |
# input_variables=["input", "format_instructions"] | |
# ) | |
self.prompt = PromptTemplate( | |
template="""\ | |
### Instruction | |
You are Trainline Mate an helpful assistant that plans tours for people at trainline.com. | |
As a smart itinerary planner with extensive knowledge of places around the | |
world, your task is to determine the user's travel destinations and any specific interests or preferences from | |
their message. | |
### Your task | |
This input is a resume of what the user wants to do. From this you have to be able to retrieve all the following information: | |
"Where is the trip to", "Start and end dates for the trip", "Is there any time constrain that you should be aware of", "activity preferences", | |
"Is there any medical condition" and "Is there a maximum distance range in which the activities have to be". | |
### If something is missing then ask for the missing information. | |
The user don't like give much information at once. So try to minimize the quantity of information that you ask for in your response. | |
Ask at maximum for information for two of the questions. | |
### Input: {input} | |
### Response: {format_instructions} | |
""", | |
input_variables=["input", "format_instructions"] | |
) | |
# Is this input complete? Does it have all the information mention before or is it missing something? If it's not complete, what is missing? | |
def format_prompt(self, input): | |
return self.prompt.format_prompt(input=input, format_instructions=self.format_instructions) | |
# return self.prompt.format_prompt(input=input) | |
def get_parsed_result(self, input): | |
result= self.model(input.to_string()) | |
parsed_result = self.output_parser.parse(result) | |
return parsed_result.answer |