Skip to content

Commit

Permalink
Ask user if they want to use gpt3.5 or gpt4 in chat with your documen…
Browse files Browse the repository at this point in the history
…ts example (#1116)

## Description of changes

Ask user if they want to use GPT-4 or GPT-3.5-turbo

*Summarize the changes made by this PR.*
 - Improvements & Bug fixes
	 - #1115 
 - New functionality
	 - Add GPT-4 to chat with your documents example

## Documentation Changes
If you need me to update the README.md in the example, please let me
know.
  • Loading branch information
BChip authored Dec 19, 2023
1 parent e0c5d44 commit 1ba6eac
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions examples/chat_with_your_documents/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def build_prompt(query: str, context: List[str]) -> List[Dict[str, str]]:
return [system, user]


def get_chatGPT_response(query: str, context: List[str]) -> str:
def get_chatGPT_response(query: str, context: List[str], model_name: str) -> str:
"""
Queries the GPT API to get a response to the question.
Expand All @@ -52,9 +52,8 @@ def get_chatGPT_response(query: str, context: List[str]) -> str:
Returns:
A response to the question.
"""

response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
model=model_name,
messages=build_prompt(query, context),
)

Expand All @@ -64,12 +63,19 @@ def get_chatGPT_response(query: str, context: List[str]) -> str:
def main(
collection_name: str = "documents_collection", persist_directory: str = "."
) -> None:

# Check if the OPENAI_API_KEY environment variable is set. Prompt the user to set it if not.
if "OPENAI_API_KEY" not in os.environ:
openai.api_key = input(
"Please enter your OpenAI API Key. You can get it from https://platform.openai.com/account/api-keys\n"
)

# Ask what model to use
model_name = "gpt-3.5-turbo"
answer = input(f"Do you want to use GPT-4? (y/n) (default is {model_name}): ")
if answer == "y":
model_name = "gpt-4"

# Instantiate a persistent chroma client in the persist_directory.
# This will automatically load any previously saved collections.
# Learn more at docs.trychroma.com
Expand All @@ -85,7 +91,7 @@ def main(
if len(query) == 0:
print("Please enter a question. Ctrl+C to Quit.\n")
continue
print("\nThinking...\n")
print(f"\nThinking using {model_name}...\n")

# Query the collection to get the 5 most relevant results
results = collection.query(
Expand All @@ -100,7 +106,7 @@ def main(
)

# Get the response from GPT
response = get_chatGPT_response(query, results["documents"][0]) # type: ignore
response = get_chatGPT_response(query, results["documents"][0], model_name) # type: ignore

# Output, with sources
print(response)
Expand Down

0 comments on commit 1ba6eac

Please sign in to comment.