Skip to content

Instantly share code, notes, and snippets.

@JitendraZaa
Created December 15, 2023 00:31
Show Gist options
  • Save JitendraZaa/2a5a31cef950c11fadd08ca2151b3194 to your computer and use it in GitHub Desktop.
Save JitendraZaa/2a5a31cef950c11fadd08ca2151b3194 to your computer and use it in GitHub Desktop.
Talk To Salesforce data using Langchain, OpenAI, Python, ChromaDB
import os
import sys
import json
import openai
from langchain.chains import ConversationalRetrievalChain, RetrievalQA
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import DirectoryLoader, TextLoader
from langchain.embeddings import OpenAIEmbeddings
from langchain.indexes import VectorstoreIndexCreator
from langchain.indexes.vectorstore import VectorStoreIndexWrapper
from langchain.llms import OpenAI
from langchain.vectorstores import Chroma
from langchain.document_loaders import JSONLoader
import numpy as np
from numpy.linalg import norm
import pandas as pd
import requests
from ast import literal_eval
import myconstants
os.environ["OPENAI_API_KEY"] = myconstants.APIKEY
# Salesforce credentials
sf_username = myconstants.SF_USERNAME
sf_password = myconstants.SF_PASSWORD
sf_security_token = myconstants.SF_SECURITY_TOKEN
sf_instance = myconstants.SF_INSTANCE
sf_instance_afterlogin = myconstants.SF_INSTANCE_AFTERLOGIN
sf_client_id = myconstants.SF_CLIENT_ID
sf_client_secret = myconstants.SF_CLIENT_SECRET
# Salesforce authentication endpoint
auth_url = f'https://{sf_instance}/services/oauth2/token'
# Salesforce REST API endpoint for querying accounts
query_url = f'https://{sf_instance_afterlogin}/services/data/v57.0/query?q=SELECT+Id,Name,Company,Title,LeadSource,Email,Status+FROM+Lead'
# Salesforce authentication payload
auth_payload = {
'grant_type': 'password',
'client_id': sf_client_id, # Replace with your Salesforce Connected App's client ID
'client_secret': sf_client_secret, # Replace with your Salesforce Connected App's client secret
'username': sf_username,
'password': sf_password + sf_security_token
}
# Authenticate with Salesforce
auth_response = requests.post(auth_url, data=auth_payload)
auth_data = auth_response.json()
access_token = auth_data['access_token']
# Query Salesforce to get account information
headers = {'Authorization': f'Bearer {access_token}'}
query_response = requests.get(query_url, headers=headers)
leads = query_response.json().get('records', [])
# Save Salesforce response as JSON file
json_filename = 'salesforce_lead_response.json'
with open(json_filename, 'w') as json_file:
json.dump(leads, json_file)
# Generate content from Salesforce response
content = "\n".join([f"Lead Name = {lead['Name']} , ID = {lead['Id']}, Company = {lead['Company']}, Title = {lead['Title']}, Email = {lead['Email']}" for lead in leads])
# Specify the file path
file_path = "salesforce_lead_response.txt"
# Open the file in write mode and write the content
with open(file_path, 'w') as file:
file.write(content)
# Enable to save to disk & reuse the model (for repeated queries on the same data)
PERSIST = False
query = None
if len(sys.argv) > 1:
query = sys.argv[1]
if PERSIST and os.path.exists("persist"):
print("Reusing index...\n")
vectorstore = Chroma(persist_directory="persist", embedding_function=OpenAIEmbeddings())
index = VectorStoreIndexWrapper(vectorstore=vectorstore)
else:
loader = TextLoader(file_path) # Use this line if you only need data.txt
#loader = DirectoryLoader("data/")
# loader = TextLoader(content)
# loader = JSONLoader( file_path='salesforce_lead_response.json', jq_schema='.', text_content=False)
if PERSIST:
index = VectorstoreIndexCreator(vectorstore_kwargs={"persist_directory":"persist"}).from_loaders([loader])
else:
index = VectorstoreIndexCreator().from_loaders([loader])
chain = ConversationalRetrievalChain.from_llm(
llm=ChatOpenAI(model="gpt-3.5-turbo"),
retriever=index.vectorstore.as_retriever(search_kwargs={"k": 1}),
)
chat_history = []
while True:
if not query:
query = input("Prompt: ")
if query in ['quit', 'q', 'exit']:
sys.exit()
result = chain({"question": query, "chat_history": chat_history})
print(result['answer'])
chat_history.append((query, result['answer']))
query = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment