anmolsahai commited on
Commit
1cbafb9
1 Parent(s): f1155da
Files changed (2) hide show
  1. langchain_pipeline.py +7 -30
  2. requirements.txt +1 -2
langchain_pipeline.py CHANGED
@@ -9,47 +9,24 @@ from langchain_openai import OpenAIEmbeddings
9
  from langchain_anthropic import ChatAnthropic
10
  from langchain_google_genai import ChatGoogleGenerativeAI
11
  import base64
12
- import jwt # Add PyJWT to your requirements
 
13
 
14
  # Load Google Cloud credentials from the environment variable
15
  credentials_json = os.getenv('GOOGLE_APPLICATION_CREDENTIALS_JSON')
16
  if credentials_json:
17
- credentials = json.loads(credentials_json)
18
  else:
19
  raise ValueError("GOOGLE_APPLICATION_CREDENTIALS_JSON environment variable not set")
20
 
21
  def get_access_token():
22
- # Request an access token using the service account credentials
23
- auth_url = 'https://oauth2.googleapis.com/token'
24
- auth_data = {
25
- 'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
26
- 'assertion': create_jwt_assertion(credentials)
27
- }
28
- response = requests.post(auth_url, data=auth_data)
29
- response.raise_for_status()
30
- return response.json()['access_token']
31
-
32
- def create_jwt_assertion(credentials):
33
- # Create a JWT assertion to use for requesting an access token
34
- header = {
35
- 'alg': 'RS256',
36
- 'typ': 'JWT'
37
- }
38
- now = int(time.time())
39
- claim_set = {
40
- 'iss': credentials['client_email'],
41
- 'scope': 'https://www.googleapis.com/auth/cloud-platform',
42
- 'aud': 'https://oauth2.googleapis.com/token',
43
- 'exp': now + 3600,
44
- 'iat': now
45
- }
46
- jwt_message = json.dumps(header) + '.' + json.dumps(claim_set)
47
- signed_jwt = jwt.encode(jwt_message, credentials['private_key'], algorithm='RS256')
48
- return signed_jwt
49
 
50
  def generate(document_parts, prompt_text):
51
  access_token = get_access_token()
52
- model_endpoint = f'https://us-central1-aiplatform.googleapis.com/v1/projects/{credentials["project_id"]}/locations/us-central1/endpoints/gemini-1.5-pro-001:predict'
53
  headers = {
54
  'Authorization': f'Bearer {access_token}',
55
  'Content-Type': 'application/json'
 
9
  from langchain_anthropic import ChatAnthropic
10
  from langchain_google_genai import ChatGoogleGenerativeAI
11
  import base64
12
+ from google.oauth2 import service_account
13
+ from google.auth.transport.requests import Request
14
 
15
  # Load Google Cloud credentials from the environment variable
16
  credentials_json = os.getenv('GOOGLE_APPLICATION_CREDENTIALS_JSON')
17
  if credentials_json:
18
+ credentials_info = json.loads(credentials_json)
19
  else:
20
  raise ValueError("GOOGLE_APPLICATION_CREDENTIALS_JSON environment variable not set")
21
 
22
  def get_access_token():
23
+ credentials = service_account.Credentials.from_service_account_info(credentials_info)
24
+ credentials.refresh(Request())
25
+ return credentials.token
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  def generate(document_parts, prompt_text):
28
  access_token = get_access_token()
29
+ model_endpoint = f'https://us-central1-aiplatform.googleapis.com/v1/projects/{credentials_info["project_id"]}/locations/us-central1/endpoints/gemini-1.5-pro-001:predict'
30
  headers = {
31
  'Authorization': f'Bearer {access_token}',
32
  'Content-Type': 'application/json'
requirements.txt CHANGED
@@ -10,5 +10,4 @@ langchain-astradb
10
  langchain-openai
11
  langchain-anthropic
12
  langchain-google-genai
13
- docxcompose
14
- pyjwt
 
10
  langchain-openai
11
  langchain-anthropic
12
  langchain-google-genai
13
+ docxcompose