Skip to content

Instantly share code, notes, and snippets.

@Cotchi666
Last active April 10, 2024 07:22
Show Gist options
  • Save Cotchi666/7c253d7329e14da1dea4d0fee17e831c to your computer and use it in GitHub Desktop.
Save Cotchi666/7c253d7329e14da1dea4d0fee17e831c to your computer and use it in GitHub Desktop.
train LLMs
import os
from dotenv import load_dotenv
from operator import itemgetter
load_dotenv()
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import CharacterTextSplitter
from langchain.document_loaders.csv_loader import CSVLoader
from langchain.docstore.document import Document
from langchain_community.vectorstores import Neo4jVector
from langchain_openai import OpenAIEmbeddings
from langchain.chat_models import ChatOpenAI
from langchain.chains import RetrievalQAWithSourcesChain
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnableLambda
embedding_model = OpenAIEmbeddings()
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
url = "neo4j+ssc://ba68e959.databases.neo4j.io"
username = "neo4j"
password = os.getenv('NEO4J_PASSWORD')
template = """Answer the question based only on the following context:
{context}
Question: {question}
Answer in the following language: {language}
When you have an Answer to response to say to the Human,create a link also, you MUST use the format:
```
<a href=\"" target=\"_blank\">our documentation</a>
```
"""
prompt = ChatPromptTemplate.from_template(template)
chain = (
{
"context": itemgetter("question") | retriever,
"question": itemgetter("question"),
"language": itemgetter("language"),
}
| prompt
| llm
)
chain.invoke({"question": "Hi bro", "language": "vietnamese"})
retrieved_docs = retriever.invoke("Hướng dẫn")
retrieved_docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment