Skip to content

Commit

Permalink
Adds Vertex as an option, query param saving (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 authored Oct 20, 2023
1 parent c64f553 commit 5dd0929
Show file tree
Hide file tree
Showing 7 changed files with 509 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deploy-production.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: superfly/flyctl-actions/setup-flyctl@master
- name: Write Google Vertex credentials file
run: echo "${{ secrets.GOOGLE_VERTEX_AI_CREDENTIALS }}" > .google_vertex_ai_credentials.json
- run: flyctl deploy --wait-timeout 600
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,5 @@ bin/
pyvenv.cfg
node_modules/
.envrc

.google_vertex_ai_credentials.json
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ If you'd like to add or swap in different base retrievers (e.g. if you want to u
export OPENAI_API_KEY=
export TAVILY_API_KEY=
# for Anthropic and Vertex models
# remove models from code if unused
ANTHROPIC_API_KEY=
GOOGLE_APPLICATION_CREDENTIALS=
# if you'd like to use the You.com retriever
export YDC_API_KEY=
Expand Down
12 changes: 9 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from langchain.callbacks.manager import CallbackManagerForRetrieverRun
from langchain.chat_models import ChatAnthropic, ChatOpenAI
from langchain.chat_models import ChatAnthropic, ChatOpenAI, ChatVertexAI
from langchain.document_loaders import AsyncHtmlLoader
from langchain.document_transformers import Html2TextTransformer
from langchain.embeddings import OpenAIEmbeddings
Expand Down Expand Up @@ -295,13 +295,19 @@ def create_chain(
model="gpt-3.5-turbo-16k",
# model="gpt-4",
streaming=True,
temperature=0,
temperature=0.1,
).configurable_alternatives(
# This gives this field an id
# When configuring the end runnable, we can then use this id to configure this field
ConfigurableField(id="llm"),
default_key="openai",
anthropic=ChatAnthropic(model="claude-2", max_tokens=16384),
anthropic=ChatAnthropic(model="claude-2", max_tokens=16384, temperature=0.1),
googlevertex=ChatVertexAI(
model_name="chat-bison-32k",
temperature=0.1,
max_output_tokens=8192,
stream=True,
),
)


Expand Down
42 changes: 37 additions & 5 deletions nextjs/app/components/ChatWindow.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import React, { useRef, useState } from "react";
import React, { useRef, useState, useEffect } from "react";
import { useSearchParams } from "next/navigation";

import { v4 as uuidv4 } from "uuid";
import { ChatMessageBubble, Message } from "./ChatMessageBubble";
Expand Down Expand Up @@ -33,13 +34,17 @@ export function ChatWindow(props: {
placeholder?: string;
titleText?: string;
}) {
const searchParams = useSearchParams();

const conversationId = uuidv4();
const messageContainerRef = useRef<HTMLDivElement | null>(null);
const [messages, setMessages] = useState<Array<Message>>([]);
const [input, setInput] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [retriever, setRetriever] = useState<RetrieverName>("tavily");
const [llm, setLlm] = useState("openai");
const [retriever, setRetriever] = useState<RetrieverName>(
(searchParams.get("retriever") as RetrieverName) ?? "tavily",
);
const [llm, setLlm] = useState(searchParams.get("llm") ?? "openai");

const [chatHistory, setChatHistory] = useState<[string, string][]>([]);

Expand Down Expand Up @@ -219,6 +224,21 @@ export function ChatWindow(props: {
await sendMessage(question);
};

const insertUrlParam = (key: string, value?: string) => {
if (window.history.pushState) {
const searchParams = new URLSearchParams(window.location.search);
searchParams.set(key, value ?? "");
const newurl =
window.location.protocol +
"//" +
window.location.host +
window.location.pathname +
"?" +
searchParams.toString();
window.history.pushState({ path: newurl }, "", newurl);
}
};

return (
<div
className={
Expand Down Expand Up @@ -250,7 +270,11 @@ export function ChatWindow(props: {
<div className="flex items-center mb-2">
<span className="shrink-0 mr-2">Powered by</span>
<Select
onChange={(e) => setRetriever(e.target.value as RetrieverName)}
value={retriever}
onChange={(e) => {
insertUrlParam("retriever", e.target.value);
setRetriever(e.target.value as RetrieverName);
}}
width={"212px"}
>
<option value="tavily">Tavily</option>
Expand All @@ -262,9 +286,17 @@ export function ChatWindow(props: {
</div>
<div className="flex items-center mb-2">
<span className="shrink-0 ml-2 mr-2">and</span>
<Select onChange={(e) => setLlm(e.target.value)} width={"212px"}>
<Select
value={llm}
onChange={(e) => {
insertUrlParam("llm", e.target.value);
setLlm(e.target.value);
}}
width={"212px"}
>
<option value="openai">GPT-3.5-Turbo</option>
<option value="anthropic">Claude-2</option>
<option value="googlevertex">Google Vertex AI</option>
</Select>
</div>
</div>
Expand Down
Loading

0 comments on commit 5dd0929

Please sign in to comment.