themlguy-tf / generative-ai Goto Github PK
View Code? Open in Web Editor NEWGCP Gen AI Playlist here
Home Page: https://www.youtube.com/watch?v=HCjE1fFlgng&list=PLyD1XCIRA3gRU_E_N863_Oiatc5EKPtp5
License: GNU General Public License v2.0
GCP Gen AI Playlist here
Home Page: https://www.youtube.com/watch?v=HCjE1fFlgng&list=PLyD1XCIRA3gRU_E_N863_Oiatc5EKPtp5
License: GNU General Public License v2.0
I'm executing this code on my local machine:
import time
from typing import Any, Mapping, List, Dict, Optional, Tuple, Union
from dataclasses import dataclass, field
from pydantic import BaseModel, Extra, root_validator
from langchain.llms.base import LLM
from langchain.embeddings.base import Embeddings
from langchain.chat_models.base import BaseChatModel
from langchain.llms.utils import enforce_stop_tokens
from langchain.schema import Generation, LLMResult
from langchain.schema import AIMessage, BaseMessage, ChatGeneration, ChatResult, HumanMessage, SystemMessage
from vertexai.preview.language_models import TextGenerationResponse, ChatSession
def rate_limit(max_per_minute):
period = 60 / max_per_minute
print('Waiting')
while True:
before = time.time()
yield
after = time.time()
elapsed = after - before
sleep_time = max(0, period - elapsed)
if sleep_time > 0:
print('.', end='')
time.sleep(sleep_time)
class _VertexCommon(BaseModel):
"""Wrapper around Vertex AI large language models.
To use, you should have the
``google.cloud.aiplatform.private_preview.language_models`` python package
installed.
"""
client: Any = None #: :meta private:
model_name: str = "text-bison@001"
"""Model name to use."""
temperature: float = 0.2
"""What sampling temperature to use."""
top_p: int = 0.8
"""Total probability mass of tokens to consider at each step."""
top_k: int = 40
"""The number of highest probability tokens to keep for top-k filtering."""
max_output_tokens: int = 200
"""The maximum number of tokens to generate in the completion."""
@property
def _default_params(self) -> Mapping[str, Any]:
"""Get the default parameters for calling Vertex AI API."""
return {
"temperature": self.temperature,
"top_p": self.top_p,
"top_k": self.top_k,
"max_output_tokens": self.max_output_tokens
}
def _predict(self, prompt: str, stop: Optional[List[str]]) -> str:
res = self.client.predict(prompt, **self._default_params)
return self._enforce_stop_words(res.text, stop)
def _enforce_stop_words(self, text: str, stop: Optional[List[str]]) -> str:
if stop:
return enforce_stop_tokens(text, stop)
return text
@property
def _llm_type(self) -> str:
"""Return type of llm."""
return "vertex_ai"
class VertexLLM(_VertexCommon, LLM):
model_name: str = "text-bison@001"
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that the python package exists in environment."""
try:
from vertexai.preview.language_models import TextGenerationModel
except ImportError:
raise ValueError(
"Could not import Vertex AI LLM python package. "
)
try:
values["client"] = TextGenerationModel.from_pretrained(values["model_name"])
except AttributeError:
raise ValueError(
"Could not set Vertex Text Model client."
)
return values
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
"""Call out to Vertex AI's create endpoint.
Args:
prompt: The prompt to pass into the model.
Returns:
The string generated by the model.
"""
return self._predict(prompt, stop)
am getting :
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.